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

hi all,

I want to run a script sitting on one machine to be ran on another remote machine. the server name, username, password will be hard coded in the script itself... so this script has to login to the other machine and it should run there.

Thanks,
Mercury.

Replies are listed 'Best First'.
Re: running script remotely
by starX (Chaplain) on Feb 01, 2008 at 17:20 UTC
Re: running script remotely
by hipowls (Curate) on Feb 02, 2008 at 11:07 UTC

    This is untested, probably unsafe and assumes unix. The basic idea is simple, if we aren't on the remote host copy ourself there otherwise do what we were written for.

    use strict; use warnings; use Sys::Hostname; chmod 0700, $0; my $remote_host = 'somewhere'; my $user_name = 'somebody'; my $password = 'something'; if ( hostname ne $remote_host ) { require Net::SFTP; require File::Basename; require Net::SSH::Perl; my $remote_file = basename $0; my $sftp = Net::SFTP->new( $remote_host, user => $user_name, password => $password ); $sftp->put( $0, $remote_file ); my $ssh = Net::SSH::Perl->new($remote_host); $ssh->login( $user_name, $password ); $ssh->cmd($remote_file); } else { unlink $0; # do your stuff here }
    I've also assumed that you can install all the required modules;-)

      thanks hipowls...

      if the script is copied to the remote machine, can we delete the script on the remote as soon as the result is returned.

        The first thing the example does is to delete itself. Once the perl interpreter has read it, it is no longer required.