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

Hello, I am fairly new to using Perl and am having trouble with getting net use to operate correctly. The program will run, but nothing will happen on the target machine.

All of the net use commands and psexec commands work when run in a batch file, so i figure that i have them formatted in-correctly for use in Perl.
#!Perl use 5.010; # Defines variables $ipInput = 0; $userName = 0; $passWord = 0; $ip = 0; # Defines the IP array @ip = (); # User input say "Please enter the username:"; $userName = <STDIN>; say ""; say "Please enter the password:"; $passWord = <STDIN>; say ""; say "Please enter the ip addresses of the machines you would like to i +nstall VNC on"; say ""; say "Ex: xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx etc..."; $ipInput = <STDIN>; # Sets delimeter for array input @ip = split(',',$ipInput); # A loop that will perform the operation on each machine, from wich an + ip was given foreach $ip(@ip) { # Initiate the connection 'net use \\\\$ip\\IPC$ /user:$userName $passWord'; # 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\\O +RL"'; # 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"'; }
Additionally, I do not know if it matters but I am using Active Perl.
Thank you for your time.

Replies are listed 'Best First'.
Re: Trouble with net use
by holli (Abbot) on Jan 30, 2009 at 17:44 UTC
    You are using single quotes (') where you want to use backtick-quotes (`). If you had used "warnings", Perl would have told you that. (Usesluss use of a constant in void context at...)


    holli

    When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.
Re: Trouble with net use
by kennethk (Abbot) on Jan 30, 2009 at 17:47 UTC

    At the start of any program, you should include use strict; use warnings;. This combo will catch a large number of bugs, including what you are hitting. You will need to declare all variables with my in order to satisfy strict, but it in turn catches a large number of typos.

    Running this with warnings returns a series of "Useless use of a constant in void context". This is because you have used single quotes (') where you probably meant to use backticks (`).

    Update: I should also mention, in addition to codeacrobat's note about chomp, that you need to escape $ as well as \ in interpolated strings - you unintentionally reference the $\ variable twice in your code. Warnings will catch that one, too.

Re: Trouble with net use
by codeacrobat (Chaplain) on Jan 30, 2009 at 18:06 UTC
    The most obvious reason that your script does nothing is because you mix up single quotes with backticks ( actually executes the command ). Also you should chomp your userinput, otherwise you also pass an ending newline in the next command. I strongly recommend, that you look at some bootstrapping perl tutorial e.g. perlintro.

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
      Thank you all for your responses.