Execute Perl script on remote server from local ma

2019-02-24 02:21发布

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.

标签: perl ssh
2条回答
2楼-- · 2019-02-24 02:52

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;
查看更多
爷的心禁止访问
3楼-- · 2019-02-24 03:07

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

查看更多
登录 后发表回答