Simon336697
asked on
Inputting the Filename for systems as a parameter for a perl script
Hi guys hope you can help.
Id like to turn the following working script, into one that I can manually type in the name of the text file, which houses the computers.
eg. findversion.pl -c <computerlist.txt>
At the moment, it uses a text file of computer names, which I have hard coded the name, which is fine, but Id like to modify the script, so I if I can put any filename in as a parameter. It's also a way so that I dont have to hardcode the text file in the script.
Here is the code.
========================== ========== ========== ========== ==
sub RunRegQuery {
my $regoutput = `reg query "\\\\$System\\HKLM\\Softwa re\\Networ k Associates\\TVD\\VirusScan Enterprise\\CurrentVersion " /v szProductVer 2>&1`;
if($regoutput =~ /Error: The system was unable to find the specified registry key or value/i){
print U "$System: Key not found!\n";
return;
}
if($regoutput =~ /Error:/){
print U "$System: Unknown error: $regoutput\n";
return;
}
my $version;
if( $regoutput =~ /REG_SZ\s+([\d.]+)/ ){
$version=$1;
}
else {
print U "$System: Unknown version: $regoutput\n";
return;
}
if($version =~ /^8\b/) {
print "$System: At Version 8! Version is $version\n";
print Y "$System: AT VERSION 8! (Version is $version)\n";
return;
}
print "$System: NOT AT VERSION 8! ! (Version is $version)\n";
print N "$System: NOT AT VERSION 8! (Version is $version)\n";
}
######
#MAIN:
######
open Y,">vs8.txt" or die "vs8.txt $!";
open N,">novs8.txt" or die "novs8.txt $!";
open U,">unknown.txt" or die "unknown.txt $!";
open (Store, "< systems.txt") or die "can't open systems: $!";
while( <Store> ) {
$System = $_;
chomp ($System);
print "\nPolling $System..........\n";
$result = system ("net use \\\\$System\\ipc\$ \"password\" /user:$System\username > NUL"); #piping to NUL - remove output.
if ($result !=0) { #if not successful
print "$System: Unreachable!\n";
print U "$System: Unreachable!\n";
}
else {
print "$System: Connected!\n";
RunRegQuery();
system ("net use \\\\$System\\ipc\$ /delete /y > 2NUL > NUL"); # Delete previous connection
}
}
========================== ========== ========== ========== ========== ===
Id like to turn the following working script, into one that I can manually type in the name of the text file, which houses the computers.
eg. findversion.pl -c <computerlist.txt>
At the moment, it uses a text file of computer names, which I have hard coded the name, which is fine, but Id like to modify the script, so I if I can put any filename in as a parameter. It's also a way so that I dont have to hardcode the text file in the script.
Here is the code.
==========================
sub RunRegQuery {
my $regoutput = `reg query "\\\\$System\\HKLM\\Softwa
if($regoutput =~ /Error: The system was unable to find the specified registry key or value/i){
print U "$System: Key not found!\n";
return;
}
if($regoutput =~ /Error:/){
print U "$System: Unknown error: $regoutput\n";
return;
}
my $version;
if( $regoutput =~ /REG_SZ\s+([\d.]+)/ ){
$version=$1;
}
else {
print U "$System: Unknown version: $regoutput\n";
return;
}
if($version =~ /^8\b/) {
print "$System: At Version 8! Version is $version\n";
print Y "$System: AT VERSION 8! (Version is $version)\n";
return;
}
print "$System: NOT AT VERSION 8! ! (Version is $version)\n";
print N "$System: NOT AT VERSION 8! (Version is $version)\n";
}
######
#MAIN:
######
open Y,">vs8.txt" or die "vs8.txt $!";
open N,">novs8.txt" or die "novs8.txt $!";
open U,">unknown.txt" or die "unknown.txt $!";
open (Store, "< systems.txt") or die "can't open systems: $!";
while( <Store> ) {
$System = $_;
chomp ($System);
print "\nPolling $System..........\n";
$result = system ("net use \\\\$System\\ipc\$ \"password\" /user:$System\username > NUL"); #piping to NUL - remove output.
if ($result !=0) { #if not successful
print "$System: Unreachable!\n";
print U "$System: Unreachable!\n";
}
else {
print "$System: Connected!\n";
RunRegQuery();
system ("net use \\\\$System\\ipc\$ /delete /y > 2NUL > NUL"); # Delete previous connection
}
}
==========================
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tintin youre BRILLIANT!!! That works fantastic.
Can I be a real pain?
Is there any way to do the following in addition?
a) If a user forgets to place a filename after the script, to prompt them?
eg.
c:>findversion.pl
!!Stop..Please enter a filename, as in "findversion systems.txt"
b) Also, if instead of a text file as the parameter, how would i change the code so that I could enter a computer name to run the script?
Youre the best.
Can I be a real pain?
Is there any way to do the following in addition?
a) If a user forgets to place a filename after the script, to prompt them?
eg.
c:>findversion.pl
!!Stop..Please enter a filename, as in "findversion systems.txt"
b) Also, if instead of a text file as the parameter, how would i change the code so that I could enter a computer name to run the script?
Youre the best.
ASKER
Sorry Tintin......a) you have already done.
Are you saying that
while( <> ){
did not work?
unless( @ARGV ){
print "!!Stop..Please enter a filename, as in "findversion systems.txt";
@ARGV = <>;
}
while( <> ){
did not work?
unless( @ARGV ){
print "!!Stop..Please enter a filename, as in "findversion systems.txt";
@ARGV = <>;
}
unless( @ARGV ){
print "!!Stop..Please enter a filename, as in "findversion systems.txt";
chomp(@ARGV = <>);
}
print "!!Stop..Please enter a filename, as in "findversion systems.txt";
chomp(@ARGV = <>);
}
ASKER
Thanks ozo, you were right....the while did work.
ASKER
The only thing now guys Id like cleared up, is to do the following...
Instead of
c:>findversion.pl <filename>
Id like to know how to modify the code so that it can do:
c:>findversion.pl <computername>
Here is the code again....
========================== ========== ========== ========== ========== =========
sub RunRegQuery {
my $regoutput = `reg query "\\\\$System\\HKLM\\Softwa re\\Networ k Associates\\TVD\\VirusScan Enterprise\\CurrentVersion " /v szProductVer 2>&1`;
if($regoutput =~ /Error: The system was unable to find the specified registry key or value/i){
print U "$System: Key not found!\n";
return;
}
if($regoutput =~ /Error:/){
print U "$System: Unknown error: $regoutput\n";
return;
}
my $version;
if( $regoutput =~ /REG_SZ\s+([\d.]+)/ ){
$version=$1;
}
else {
print U "$System: Unknown version: $regoutput\n";
return;
}
if($version =~ /^8\b/) {
print "$System: At Version 8! Version is $version\n";
print Y "$System: AT VERSION 8! (Version is $version)\n";
return;
}
print "$System: NOT AT VERSION 8! Install Version 8 (Version is $version)\n";
print N "$System: NOT AT VERSION 8! (Version is $version)\n";
}
######
#MAIN:
######
open Y,">vs8.txt" or die "vs8.txt $!";
open N,">novs8.txt" or die "novs8.txt $!";
open U,">unknown.txt" or die "unknown.txt $!";
my $file = shift or die "Usage: $0 [filename]\n";
open Store, $file or die "Can not open $file: $!";
while( <Store> ) {
$System = $_;
chomp ($System);
print "\nPolling $System..........\n";
$result = system ("net use \\\\$System\\ipc\$ \"password" /user:$System\\username > NUL"); #piping to NUL - remove output.
if ($result !=0) { #if not successful
print "$System: Unreachable!\n";
print U "$System: Unreachable!\n";
}
else {
print "$System: Connected!\n";
RunRegQuery();
system ("net use \\\\$System\\ipc\$ /delete /y > 2NUL > NUL"); # Delete previous connection
}
}
========================== ========== ========== ========= eof
Instead of
c:>findversion.pl <filename>
Id like to know how to modify the code so that it can do:
c:>findversion.pl <computername>
Here is the code again....
==========================
sub RunRegQuery {
my $regoutput = `reg query "\\\\$System\\HKLM\\Softwa
if($regoutput =~ /Error: The system was unable to find the specified registry key or value/i){
print U "$System: Key not found!\n";
return;
}
if($regoutput =~ /Error:/){
print U "$System: Unknown error: $regoutput\n";
return;
}
my $version;
if( $regoutput =~ /REG_SZ\s+([\d.]+)/ ){
$version=$1;
}
else {
print U "$System: Unknown version: $regoutput\n";
return;
}
if($version =~ /^8\b/) {
print "$System: At Version 8! Version is $version\n";
print Y "$System: AT VERSION 8! (Version is $version)\n";
return;
}
print "$System: NOT AT VERSION 8! Install Version 8 (Version is $version)\n";
print N "$System: NOT AT VERSION 8! (Version is $version)\n";
}
######
#MAIN:
######
open Y,">vs8.txt" or die "vs8.txt $!";
open N,">novs8.txt" or die "novs8.txt $!";
open U,">unknown.txt" or die "unknown.txt $!";
my $file = shift or die "Usage: $0 [filename]\n";
open Store, $file or die "Can not open $file: $!";
while( <Store> ) {
$System = $_;
chomp ($System);
print "\nPolling $System..........\n";
$result = system ("net use \\\\$System\\ipc\$ \"password" /user:$System\\username > NUL"); #piping to NUL - remove output.
if ($result !=0) { #if not successful
print "$System: Unreachable!\n";
print U "$System: Unreachable!\n";
}
else {
print "$System: Connected!\n";
RunRegQuery();
system ("net use \\\\$System\\ipc\$ /delete /y > 2NUL > NUL"); # Delete previous connection
}
}
==========================
What do you want to do with the computer name?
How would you have hard coded the computer name?
How would you have hard coded the computer name?
ASKER
Ozo!
In the script, the computername is called $System.
In the script, the computername is called $System.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works FANTASTIC!!!!!!!!!!!
Thank you magicians! to Ozo and Tintin....A HUGE THANKYOU!!!
S
Thank you magicians! to Ozo and Tintin....A HUGE THANKYOU!!!
S
# open (Store, "< systems.txt") or die "can't open systems: $!";
# while( <Store> ) {
while( <> ){