Tsamaunk
Tsamaunk

Reputation: 68

Execute Perl script on remote server from local machine

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.

Upvotes: 4

Views: 16218

Answers (2)

mpapec
mpapec

Reputation: 50667

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;

Upvotes: 3

Steve Sanbeg
Steve Sanbeg

Reputation: 887

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

Upvotes: 4

Related Questions