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.