Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

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\\Software\\Network 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
   }
}
=====================================================================
Avatar of ozo
ozo
Flag of United States of America image

findversion.pl   systems.txt


# open (Store, "< systems.txt") or die "can't open systems: $!";
# while( <Store> ) {
while( <> ){
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

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.
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 = <>;
}
unless( @ARGV ){
  print "!!Stop..Please enter a filename, as in "findversion systems.txt";
  chomp(@ARGV = <>);
}
Thanks ozo, you were right....the while did work.
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\\Software\\Network 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
What do you want to do with the computer name?
How would you have hard coded the computer name?
Ozo!

In the script, the computername is called $System.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works FANTASTIC!!!!!!!!!!!

Thank you magicians! to Ozo and Tintin....A HUGE THANKYOU!!!

S