Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

run a perl script on a unix machine from a win machine

by akrrs7 (Acolyte)
on Dec 01, 2011 at 15:20 UTC ( #941090=perlquestion: print w/ replies, xml ) Need Help??
akrrs7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I would like to run a perl script that sits on my win machine (client) on a unix server. I have putty/plink etc. setup where from a win command prompt I can execute any command on the unix server (using rsa keys for ssh authentication). I have the following in my perl script:
system("plink $server"); rest of my perl script
However, it does log in fine but does not execute the rest of the perl script. Also how can I use perl to get the permissions on a file and print the same to a text file. This is my code:
chdir ($pathToCheck); open theInputFile, $inputFile or die "Could not read from $inputFi +le, program halting. \n"; open (theOutputFile, '>', $outputFile) or die "Could not open $out +putFile. \n"; while (<theInputFile>) { chomp; my $fileBeingChecked = "$_"; if (-e $fileBeingChecked) { #my $fileSize = -s $fileBeingChecked; my $fileSize = stat($fileBeingChecked)->size; my $date_string = ctime(stat($fileBeingChecked)->mtime); print theOutputFile "Yes, $fileSize, $date_string \n"; } else { print theOutputFile "No \n"; } } close (theOutputFile); close (theInputFile);
I would like to add the permissions on the file - whether write or read etc. also to the theOutputFile. Any suggestions ?

Comment on run a perl script on a unix machine from a win machine
Select or Download Code
Re: run a perl script on a unix machine from a win machine
by salva (Monsignor) on Dec 01, 2011 at 15:31 UTC
    system("plink $server");
    That starts a new interactive shell on the remote system and it just keeps waiting for you to type some command at the console. What you probably want is:
    system "plink $server $cmd";
    BTW, check also Net::SSH2.
      I'm guessing the $cmd can also be some function ??? The commands are the rest of the perl script...which I could lump as a single function.

        For clarificaiton, are you expecting to use plink (via system) to connect to the unix server then the remainder of your perl script should be executed by the unix server?

        No, it doesn't work that way. You can only run shell commands there, not Perl code.

        Though you can copy some Perl script there using scp and then run it, or even pass some perl code via stdio to a perl interpreter running on the remote machine.

        For instance:

        open(my $pipe, "plink /usr/bin/perl </some/local/script.pl |") or die "unable to run command: $!"; while (<$pipe>) { print "response: $_"; }
Re: run a perl script on a unix machine from a win machine
by sumeetgrover (Scribe) on Dec 01, 2011 at 15:34 UTC
    Why don't you simply upload the script from Windows to Unix server via scp etc. and run it directly on UNIX?
      Because it needs to be run on n number of servers and I need the results on my win desktop. It would be a pain to upload the script on each server and get the results file back to my machine each time I want to run the script.

        Write a deploy script that copies the work script to all servers. Something like this:

        #!/usr/bin/perl use strict; use warnings; foreach my $server qw( oldserver newserver strangeserver ) { system "pscp workscript.pl user\@$server:/tmp/workscript.pl"; }

        For instant results, add this to the foreach loop:

        system "plink user\@$server perl /tmp/workscript.pl -foo -bar -kab +oom";

        Or use one of the SSH modules available at CPAN.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Well, that's how the world works. You got to have the program on the machine where you want to run it. There's absolutely no way to run a program that's one one machine on another without first transferring the program. Regardless of what OS the machines runs, or in which language the program is written in.

        It's like wanting to read a book that's in the library. You first get to check out the book...

Re: run a perl script on a unix machine from a win machine
by graff (Chancellor) on Dec 03, 2011 at 00:24 UTC
    What information from various unix/linux servers are you trying to get that isn't provided by "ls -l" ? Based on your code snippet, you're getting size, mod.date, and you want permissions too, and "ls -l" gives you all that (plus user and group ownership and link count).

    If you have a list of servers to look at (e.g. in a file called "server.list" on your mswin machine), and a list of files to look for on each server (e.g. in separate files called "{servername}.filelist", also on the mswin machine), an easy way get what you want via a perl script on the mswin machine might be something like:

    #!/usr/bin/perl # (standard shebang line, in case you get to use this on unix/linux so +meday) use strict; use warnings; open( SLIST, '<', "server.list" ) or die "server.list: $!\n"; while ( my $server = <SLIST>) { chomp $server; open( FLIST, '<', $server.filelist ) or do { warn "$server.filelist: $!\n"; next; } my $srvr_cmd = "ls -l"; while ( <FLIST> ) { chomp; $srvr_cmd .= " $_"; } my $file_info = `ssh $server $srvr_cmd`; print "=== results from $server ===\n$file_info\n"; }
    You should be using a shell on the mswin machine that allows for redirection of stdout to a chosen file, or you can open an explicit output file in the perl script.

    If you're trying to do something more complicated or subtle, and the above doesn't help with that, please be more explicit about what you're really trying to accomplish.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://941090]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-08-28 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (257 votes), past polls