How can I ssh inside a Perl script?

2019-01-17 23:05发布

I want to SSH to a server and execute a simple command like "id" and get the output of it and store it to a file on my primary server. I do not have privileges to install Net::SSH which would make my task very easy. Please provide me a solution for this. I tried using back-ticks but I am not able to store the output on the machine from which my script runs.

标签: perl ssh
9条回答
孤傲高冷的网名
2楼-- · 2019-01-17 23:28

If you have ssh host keys setup you can simply run the ssh system command and then specify the command to run on the machine after that. For example:

`ssh user@remoteserver.domain.com id`

You should be able to chomp/store that output.

查看更多
倾城 Initia
3楼-- · 2019-01-17 23:30

You can always install modules locally, and that is the method you should look into; however, you should be able to get away with

#!/usr/bin/perl

use strict;
use warnings;

my $id = qx/ssh remotehost id 2>&1/;

chomp $id;

print "id is [$id]\n"
查看更多
一夜七次
4楼-- · 2019-01-17 23:31

If you're using backticks try this:

my @output = `ssh root@1.1.1.1 "which perl"`;

print "output: @output";

This is only useful if you have a publickey that the above command won't prompt for password.

查看更多
混吃等死
5楼-- · 2019-01-17 23:31

Assuming that you're in an environment like me where you can't add additional modules and you can't create an Identity file, then you can use this script as a starting point.

If you can set up ssh keys then simply use the backticks command already posted, although you might need the -i option

#!/usr/bin/perl
use warnings;
use strict;
use Expect;
use Data::Dumper;

my $user = 'user';
my $pw = 'password';
my $host = 'host';
my $cmd = 'id';

my $exp = new Expect;
$exp->log_file("SSHLOGFILE.txt");
$exp->log_stdout(0);
$exp->raw_pty(1);


my $cli = "/usr/bin/ssh $user\@$host -o StrictHostKeyChecking=no -q $cmd";

$exp->spawn($cli) or die "Cannot spawn $cli: $!\n";

$exp->expect(5,
 [ qr /ssword:*/ => sub { my $exph = shift;
                          $exph->send("$pw\n");
                          exp_continue; }] );

my $read = $exp->exp_before();
chomp $read;
print Dumper($read);

$exp->soft_close();
查看更多
Bombasti
6楼-- · 2019-01-17 23:36

or, assuming host keys are present and you want to do something with the command ouput ...

open(SSH,"/usr/bin/ssh you\@server ps aux |") or die "$!\n";
while (<SSH>) { 
   # do stuff with $_ 
}
close SSH;
查看更多
迷人小祖宗
7楼-- · 2019-01-17 23:39

I know this is a very old thread, but since I encounter the same problem I found another useful solution in case that someone is using Linux.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $host = $ARGV[0];
my $port = $ARGV[1];
my $cmd = $ARGV[2];

my @output = readpipe( "ssh -p ".
               $port." ".
               $host." ".
               $cmd."" );

chomp @output;

print Dumper \@output;

__END__

perl sample.pl 127.0.0.1 22 "which perl"
Ubuntu 16.04.1 LTS
$VAR1 = [
          '/usr/bin/perl'
        ];

This assumes that you have configured ssh-keys so no user input will be required. I did not want to have hard coded values this is the best way for me that worked the best. I am using readpipe to achieve that.

Hope this helps to have a solution in case of not hard coding.

查看更多
登录 后发表回答