?
Solved

Inputting the Filename for systems as a parameter for a perl script

Posted on 2007-07-24
12
Medium Priority
?
203 Views
Last Modified: 2010-04-16
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
   }
}
=====================================================================
0
Comment
Question by:Simon336697
  • 6
  • 5
12 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 19554160
findversion.pl   systems.txt


# open (Store, "< systems.txt") or die "can't open systems: $!";
# while( <Store> ) {
while( <> ){
0
 
LVL 48

Accepted Solution

by:
Tintin earned 1000 total points
ID: 19554219
Change

open (Store, "< systems.txt") or die "can't open systems: $!";

to

my $file = shift or die "Usage: $0 [filename]\n";
open Store, $file or die "Can not open $file: $!";
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19554331
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Simon336697
ID: 19554370
Sorry Tintin......a) you have already done.
0
 
LVL 85

Expert Comment

by:ozo
ID: 19554372
Are you saying that
while( <> ){
did not work?

unless( @ARGV ){
  print "!!Stop..Please enter a filename, as in "findversion systems.txt";
  @ARGV = <>;
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 19554377
unless( @ARGV ){
  print "!!Stop..Please enter a filename, as in "findversion systems.txt";
  chomp(@ARGV = <>);
}
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19554391
Thanks ozo, you were right....the while did work.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19554414
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
0
 
LVL 85

Expert Comment

by:ozo
ID: 19554431
What do you want to do with the computer name?
How would you have hard coded the computer name?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19554449
Ozo!

In the script, the computername is called $System.
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 1000 total points
ID: 19554482
$System = shift or die "!!Stop..Please enter a filename";
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19554542
Works FANTASTIC!!!!!!!!!!!

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

S
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Six Sigma Control Plans
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question