Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Net Use issues by Lobus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (8)
    As of 2018-11-15 14:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My code is most likely broken because:
















      Results (186 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!