http://www.perlmonks.org?node_id=743995

Lobus has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

I am having an issue with the following code. A user selects if they want to enter a large group of IP addresses seperated by commas, or if they would like to point the program to a file for IP addresses.

The program then takes the addresses and stores them in an array. Additionally the user enters a username and password. The script is then supposed to try and connect to each address and then copy some files (in this case it is Ultra VNC).

However the problem i am having is that the script only works for the first IP address in either version. After the loop runs the first time every subsequent time it tells me the username and password are wrong, even though they are correct.

Ex: If i put a the working IP address first followed by a false address, the first machine will receive VNC. But if I put a false address first, and then the correct address second... it will fail.

This is designed to run on windows, additionally psexec.exe from the pstools suite must be in the same directory. Additionally my machine has Active perl.

Thank you for your time.
#!Perl use 5.010; use strict; use warnings; # Clears the screen system("cls"); say ""; say " *---------------------------------------------*" +; say " *Welcome to Team Starcraft VNC rollout program*" +; say " *---------------------------------------------*" +; say ""; say ""; say ""; say ""; # Defines variables my $ipInput = 0; my $userName = 0; my $passWord = 0; my $ip = 0; my $choice = 0; my $fileName = 0; my $resultOutput = "Results.txt"; # Defines the IP arrays my @ip = (); my @pingFail = (); my @badUnPass = (); my @vncSuccess = (); # User picks what version to run say "How would you like input IP addresses?\n"; say "(Option #1) Enter every IP Ex: xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, +etc...\n"; say "(Option #2) Enter the name of a file that contains a list of IP a +ddresses\n"; say "Please select 1 or 2\n"; chomp($choice = <STDIN>); #Options if ($choice == 1) { # User input say "Please enter the username:\n"; chomp($userName = <STDIN>); say ""; say "Please enter the password:\n"; chomp($passWord = <STDIN>); say ""; say "Please enter the ip addresses of the machines you would like to i +nstall VNC on\n"; say "Ex: xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, etc...\n"; chomp($ipInput = <STDIN>); # Sets delimeter for array input @ip = split(',',$ipInput); @pingFail = split(',',$ip); @badUnPass = split(',',$ip); @vncSuccess = split(',',$ip); # Clears the screen system("cls"); # A loop that will perform the operation on each machine, from wich an + ip was given foreach $ip(@ip) { my $Result = system("ping $ip -w 300"); if ($Result == 0) { # Initiate the connection $Result = system("net use \\\\$ip\\IPC\$ /user:$userName $passWord"); if ($Result == 0) { # Copies the program files `xcopy "C:\\Program Files\\UltraVNC\\*.*" "\\\\$ip\\C\$\\Program Files +\\UltraVNC\\*.*" /r/i/c/h/k/e/Y`; # Dumps the registry `regedit /e "\\\\$ip\\C\$\\vncdmp.txt" "HKEY_LOCAL_MACHINE\\Software\\ +ORL"`; # Puts registry entries on remote machine `psexec \\\\$ip -s -i -d %windir%\\regedit /s C:\\vncdmp.txt`; # Installs the program as a service `psexec \\\\$ip -s -i -d "C:\\Program Files\\UltraVNC\\winvnc.exe" -in +stall`; # Starts the VNC server `psexec \\\\$ip -s -i -d net start VNC Server`; #This puts the IP address that deployed propery into an array push(@vncSuccess,$ip); say ""; say " *---------------------------------------------*" +; say " VNC deployed"; say " *---------------------------------------------*" +; } else { #This puts the IP address that could not be propery connected to into +an array push(@badUnPass,$ip); say ""; say " *---------------------------------------------*" +; say " Bad username or password"; say " *---------------------------------------------*" +; } } else { #This puts the IP addresses that could not be reached via ping into an + array push(@pingFail,$ip); say ""; say " *---------------------------------------------*" +; say " Unable to reach:"; say " $ip"; say " *---------------------------------------------*" +; } # Ends foreach $ip(@ip) { } # Ends if ($choice == 1) { } elsif ($choice == 2) { # User input say "Please enter the username:\n"; chomp($userName = <STDIN>); say ""; say "Please enter the password:\n"; chomp($passWord = <STDIN>); say ""; say "Please enter the name of a file that contains a list of IP addres +ses.\n"; say "The syntax is xxxx.xxx"; chomp($fileName=<STDIN>); open(DAT, $fileName) || die("Could not find file!"); $ipInput=<DAT>; close(DAT); # Sets delimeter for array input @ip = split(',',$ipInput); @pingFail = split(',',$ip); @badUnPass = split(',',$ip); @vncSuccess = split(',',$ip); # Clears the screen system("cls"); # A loop that will perform the operation on each machine, from wich an + ip was given foreach $ip(@ip) { my $Result = system("ping $ip -w 300"); if ($Result == 0) { # Initiate the connection $Result = system("net use \\\\$ip\\IPC\$ /user:$userName $passWord"); if ($Result == 0) { # Copies the program files `xcopy "C:\\Program Files\\UltraVNC\\*.*" "\\\\$ip\\C\$\\Program Files +\\UltraVNC\\*.*" /r/i/c/h/k/e/Y`; # Dumps the registry `regedit /e "\\\\$ip\\C\$\\vncdmp.txt" "HKEY_LOCAL_MACHINE\\Software\\ +ORL"`; # Puts registry entries on remote machine `psexec \\\\$ip -s -i -d %windir%\\regedit /s C:\\vncdmp.txt`; # Installs the program as a service `psexec \\\\$ip -s -i -d "C:\\Program Files\\UltraVNC\\winvnc.exe" -in +stall`; # Starts the VNC server `psexec \\\\$ip -s -i -d net start VNC Server`; #This puts the IP address that deployed propery into an array push(@vncSuccess,$ip); say ""; say " *---------------------------------------------*" +; say " VNC deployed"; say " *---------------------------------------------*" +; } else { #This puts the IP address that could not be propery connected to into +an array push(@badUnPass,$ip); say ""; say " *---------------------------------------------*" +; say " Bad username or password"; say " *---------------------------------------------*" +; } } else { #This puts the IP addresses that could not be reached via ping into an + array push(@pingFail,$ip); say ""; say " *---------------------------------------------*" +; say " Unable to reach:"; say " $ip"; say " *---------------------------------------------*" +; } # Ends foreach $ip(@ip) { } } else { system("cls"); say ""; say " *---------------------------------------------*" +; say " Bad input"; say " Please only select 1 or 2"; say " *---------------------------------------------*" +; system("pause"); system("perl VNC-push.pl"); } open(DAT,">$resultOutput") || die("Cannot Open File"); print DAT "VNC successfully deployed on the following machines:\n"; print DAT "\n"; close(DAT); shift(@vncSuccess); foreach $ip(@vncSuccess) { open(DAT,">>$resultOutput") || die("Cannot Open File"); print DAT "$ip\n"; print DAT "\n"; close(DAT); } open(DAT,">>$resultOutput") || die("Cannot Open File"); print DAT "Could not reach the following addresses:\n"; print DAT "\n"; close(DAT); shift(@pingFail); foreach $ip(@pingFail) { open(DAT,">>$resultOutput") || die("Cannot Open File"); print DAT "$ip\n"; print DAT "\n"; close(DAT); } open(DAT,">>$resultOutput") || die("Cannot Open File"); print DAT "Bad username or password on the following machines:\n"; print DAT "\n"; close(DAT); shift(@badUnPass); foreach $ip(@badUnPass) { open(DAT,">>$resultOutput") || die("Cannot Open File"); print DAT "$ip\n"; print DAT "\n"; close(DAT); } # By Kevin Davies.

Replies are listed 'Best First'.
Re: Net Use issues
by missingthepoint (Friar) on Feb 16, 2009 at 09:44 UTC

    Honestly, I find it very hard to read your code (and so, hard to debug) because it's not structured well. Consider refactoring it a bit, so you have something like this:

    # Top level my $choice = get_choice(); do_ip_manual() if $choice == 1; do_ip_fromfile() if $choice == 2; # Lower level sub do_ip_manual { my ($user, $pass) = get_auth(); ... } # Even lower level ...

    ... instead of lots of long if/else blocks.

    It'd be easier to debug failures if you checked the return values of all those external executions. You're assuming they succeed.

    my $ret = system "`xcopy "C:\\Program Files\\UltraVNC\\*.*" "\\\\$ip\\ +C\$\\Program Files\\UltraVNC\\*.*" /r/i/c/h/k/e/Y"; die "xcopy failed" if $ret; $ret = 0; ...

    If you'd like to improve your code generally, here are a few tips (good on you for using strict and warnings, you've just saved yourself a handful of head hair):

    open(DAT,">>$resultOutput") || die("Cannot Open File"); print DAT "Bad username or password on the following machines:\n";

    Be careful using || when performing error checking. It's a better idea to use or. You'll get bitten by precedence if you do this:

    open DAT, ">>$filename" || die "foo";

    Because || has higher precedence than the function call or the comma operator, that won't die if the open fails. It's evaluated like this:

    open DAT, (">>$filename" || die "foo");

    ... and ">>$filename" is never logically false.

    Also: it's good to get into the habit of using lexical filehandles:

    open(my $dat, ">>", $resultOutput") or die("Cannot Open File"); print $dat "Bad username or password on the following machines:\n";

    ... for reasons described on this Perl5Wiki page.


    That that is is that that is not is not
      Thank you for all of your help. I forgot to mention that I am rather new to using perl. The only part of your help that i am really having trouble with comprehending is:
      # Top level my $choice = get_choice(); do_ip_manual() if $choice == 1; do_ip_fromfile() if $choice == 2; # Lower level sub do_ip_manual { my ($user, $pass) = get_auth(); ... } # Even lower level ...
      I do not understand how to correctly use the subroutines.
Re: Net Use issues
by Anonymous Monk on Feb 16, 2009 at 07:50 UTC
    You are assuming that # Copies the program files, #Dumps the registry ... actually succeed.
      The entire program works, but only for the first IP address that is entered.
        In other words it doesn't work :)
Re: Net Use issues
by jasonk (Parson) on Feb 16, 2009 at 17:25 UTC

    You instruct the user to enter the ip addresses as 'xxx.xxx.xxx.xx, xxx.xxx.xxx.xxx' but then you split them just on ','. If the user follows your instructions, then all the ip addresses after the first one have a leading space, which means \\\\$ip\\ becomes \\\\ $ip\\, which fails, but you never find out if fails because (like everyone else said) you never check whether the commands succeeded or not.


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
      Thank you all for your help. I have remade the script using subs as suggested, additionally i have incorporated a trim sub to remove spaces.