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

Hi....
Here's the picture:
I have a script running on one machine (script1) and another script is resident on a remote machine (script2).
The first script (script1) needs to accept some values from the user and pass it on to the second script (script2) which will process based on the data. The second script, after its done, will populate some data in a text file on the same machine where it resides. I would like to know, how to implement the functionality in perl...

would appreciate any help...thanks!.

Replies are listed 'Best First'.
Re: Connecting to a remote host
by Abigail-II (Bishop) on Apr 04, 2003 at 11:06 UTC
    system ssh => 'user@remote.host', "script '$data'";

    Or you could have script2 listen to a socket, use inetd, use RPC, use SOAP, send the data by mail and use procmail on the remote end, use NFS, put script2 in a boot script and have script1 powercycle the remote host.

    The possibilities are endless, and largely depend on your infrastructure. It's also a very non-Perl issue.

    Abigail

Re: Connecting to a remote host
by robartes (Priest) on Apr 04, 2003 at 11:02 UTC
    Have a look at Net::SSH to securely connect to and interact with remote hosts. Basically you would have script1 one send on the values concerned through a ssh connection to script2, which would take these values as command line arguments and run with them.

    Update: SSH is all nice and cuddly, but I like Abigail-II's last option better (see below) :)

    CU
    Robartes-

Re: Connecting to a remote host
by tachyon (Chancellor) on Apr 04, 2003 at 11:17 UTC

    If script 2 can be a CGI just send it the values by calling it with the appropriate query string:

    # in script 1 use LWP::Simple; my %values = ( foo => 'some val', bar => 'other val' ); my $script2 = 'http://somewhere.com/cgi-bin/script2.pl'; my $query_string = join '&', map { '$_=' . escapeURL($values{$_}) } ke +ys %values; my $res = get( $script2 . '?' . $query_string ); print $res; sub escapeURL { my $encode = shift; return undef unless defined $encode; $encode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; return $encode; } # in script 2 use CGI; my $q = new CGI; print $q->header; # prove we got it.... print "<p>Got: $_ " . $q->param($_) for $q->param(); # print stuff to file....

    Alternatively use Net::SSH or IO::Socket or shell out to system('ssh', 'user@machine', 'script', 'data' ). There are lotsa ways to do it.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Hey tachyon,
      Thanks for the info mate!...
      but listen, regarding the script you had posted, how do you know whether the second script is being executed or not??
      I tried to print some data into a file in the remote machine, but the file ain't getting created!
      'twould be great if yoo could help...thanks!

        You should be able to call script2.pl like: http://mysite.com/cgi-bin/script2.pl?sent=Hello+World by typing that into the browser Address bar, and it should print something like Got sent = 'Hello World' Can't remember what format I had it printing but it was something like that. Once it works ie shows you what you sent it in the query string it will do the same when you call it using LWP::Simple get() method with the same URL. You can then just get it to print 'OK' which you then get back in $res:

        use LWP::Simple; my $res = get( 'http://mysite.com/cgi-bin/script2.pl?sent=Hello+World' + ); if ( $res eq 'OK' ) { # blah }

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

If you do go with ssh..
by Improv (Pilgrim) on Apr 04, 2003 at 13:11 UTC
    As a side note for if you go with ssh, you'll probably want to set up key-based authentication so your ssh commands will handle the logins in an automated fashion. To do so, on machine1, run:
    ssh-keygen -t rsa
    Then, go into ~/.ssh, and scp the file id_rsa.pub to machine2. Then, login to machine2, and do a
    cat id_rsa.pub >> ~/.ssh/authorized_keys
    That should let all your ssh connections from machine1 to machine2, using those accounts, use the key-based authentication instead of prompting for a password. I hope this is useful.
      great! but the thing is, i have to talk to the remote script in Windows NT...where i think ssh wont work?
        If you're using windows, I recommend installing a package called Cygwin, which gets you a decent subset of the goodness of Unix. It's free, it's fairly easy to install, and depending on what you install, you could use XFree86, perl, bash, gcc, and all that other fun stuff (including ssh).
        I don't have problems running ssh from Cygwin, which, AFAIK, runs also on Windows NT.

        Abigail

      Yeah, it won't ask for a password then, but it will ask for a passphrase instead. Now, it's possible to set an empty passphrase, such that it won't ask for one, but I strongly advice against that. That will weaken your security, because if machine1 is compromised, machine2 is as well.

      Abigail

        you can use key-agent and only have to type your password once per login and still keep your keys encrypted.

        Surely it weakens security because a single return is often the first thing Evil People will try if they do manage to get a copy of your private key.

        If they've compromised the machine running the SSH client you're in trouble regardless of the authentication method used since the Evil Person can just install a key logger and get your passwords that way.

Re: Connecting to a remote host
by nothingmuch (Priest) on Apr 04, 2003 at 12:36 UTC
    A scalable option would be to have script 2 be wrapped by a forking server. The server listens, and when an incomming connection comes on the socket it forks. Data can be read from the socket, placed on the other end by using Storable, which allows seriallizing in a network safe format. This will allow you to simply pipe data, with arbitrary complexity, to the other script, without relying on other software. This is not a safe method though, and you have to make sure both script 1 and script 2 know what data comes when.

    -nuffin
    zz zZ Z Z #!perl