Execute Perl script on remote server from local machine

14,776

Solution 1

If you're not passing arguments to the script, it should be as simple as

ssh "user@host" perl < $crawler

If you want to pass command line args, just use a "-" placeholder to get the same affect as the bash -s, i.e.

ssh "user@host" 'perl - arg1 arg2' < $crawler

Solution 2

Net::SSH is ssh executable wrapper, and there are better solutions for perl nowadays.

http://search.cpan.org/~remi/Net-SSH2-Simple/lib/Net/SSH2/Simple.pm#SYNOPSIS

my ($stdout,$stderr, $exitcode) = $ssh2->cmd("perl -e $crawler") 
  or die $ssh2->error;
Share:
14,776

Related videos on Youtube

Tsamaunk
Author by

Tsamaunk

AIX Systems Administrator

Updated on September 14, 2022

Comments

  • Tsamaunk
    Tsamaunk over 1 year

    I am rather new to Perl and translating some bash scripts to parse Apache logs from several remote production servers into a more useful format for processing on a local machine. I need to keep the production servers as uncluttered as possible. As such, I have two separate .pl files on my local machine - crawler.pl (formerly crawler.sh), responsible for the grabbing the useful information from the production servers, and reporter.pl (formerly reporter.sh), responsible for running the crawler and collating the information retrieved from the production servers.

    What I'm looking to accomplish is the Perl equivalent of:

        ssh "user@host" 'bash -s' < $crawler
    

    where $crawler is (was) the physical location of crawler.sh on the local box.

    I want reporter.pl to open an ssh connection the the production servers and remotely execute crawler.pl., something to the effect of:

        use Net::SSH qw(ssh);
        ssh($loginString,"perl -e $crawler");
    

    which obviously doesn't work, but hopefully you understand what I'm driving at.

    I know it's possible to accomplish this by copying the crawler file to the production servers and deleting it when it has completed its tasks; I'm after a more elegant solution, should one exist.

  • Tsamaunk
    Tsamaunk over 10 years
    In this case, does Perl look for $crawler on the remote host or the local machine?
  • Tsamaunk
    Tsamaunk over 10 years
    This works perfectly, though I had hoped to avoid a call to system(). Is this possible?
  • mpapec
    mpapec over 10 years
    @Tsamaunk everything is remote, btw -e $crawler implies perl script on command line
  • Steve Sanbeg
    Steve Sanbeg over 10 years
    If you want to avoid system() entirely, it looks like the CPAN module Net::SSH::Perl has a pure perl ssh client that will do that. If you just want the system call wrapped in a module, then you can continue with Net::SSH. You just need to call perl as above, and pass the contents of the script to its standard input. In Net::SSH, it looks like sshopen2 would work there; you could open the script and write it into WRITER line by line.