0

As clearly explained in the question, kindly give me an option to ssh to a machine using perl script, run a few script lines there, and return the output to the source machine.

Found out a few docs online on this, but nothing seems to be informative/clear.

Please help.

  • 1
    What didn't Googling "perl ssh" answer? It'll give you this example: http://stackoverflow.com/questions/2848725/how-can-i-ssh-inside-a-perl-script What more do you need? – Oli Jun 09 '15 at 09:14
  • I had seen these answers but still, I am afraid, I cannot figure-out how can I run a command remotely and take the output back into the current running script to complete the execution. Please specify that too....! – kiran bbnl Jun 09 '15 at 11:56

2 Answers2

5

I agree that the current examples look helpful but don't always work! My previous examples are included in that. I'm not a Perl expert at all but I've cobbled the following together and it works for me:

#!/usr/bin/perl -w
use strict;

use Net::SSH2;

my $ssh2 = Net::SSH2->new();
$ssh2->connect('tank') or die $!;
my $auth = $ssh2->auth_publickey(
    'oli',
    '/home/oli/.ssh/id_rsa'
);

my $chan2 = $ssh2->channel();
$chan2->blocking(1);

# This is where we send the command and read the response
$chan2->exec("uname -a\n");
print "$_" while <$chan2>;

$chan2->close;

Obviously you'll want to change the hostname, username and location of your RSA/DSA/etc key. Additionally, you'll need the compat library to make this work well on Ubuntu:

sudo apt-get install libnet-openssh-compat-perl

And finally —assuming you call it script.pl— call it like so:

perl -MNet::OpenSSH::Compat=Net::SSH2 script.pl
Oli
  • 293,335
2

You could always just run a system call from within your shell script. As long as you can ssh from the shell, you don't need to install anything:

perl -e '$r=`ssh user\@host hostname`; print "Remote host:$r"'

If you need to run this automatically, you have a few options:

  1. Do it cleanly as described in Oli's answer.
  2. Set up passwordless, key-based ssh authentication.
  3. Use sshpass

    First, install it from the repositories:

    sudo apt-get install sshpass
    

    Then run this:

    perl -e '$r=`sshpass -p "password" ssh user\@host hostname`; print "Remote host:$r"'
    
terdon
  • 100,812
  • Works, but this command returns with a password request. The program should be able to be ran in a cron job without a user inputting the password manually. Can this be done..? – kiran bbnl Jun 09 '15 at 10:54
  • @kiranbbnl yes, see updated answer. – terdon Jun 09 '15 at 11:02
  • Net::SSH supports keyfiles so using one directly would be preferable to embedding a password somewhere IMO. I've added another chunk to my answer. – Oli Jun 09 '15 at 11:15
  • @terdon : perl -e '$r=ssh pass -p "AcN1234" ssh admin@172.20.20.12 sysserver; print "Remote host:$r"' This is also not working.. Is there any mistake in the command that I had typed..? – – kiran bbnl Jun 09 '15 at 11:18
  • @kiranbbnl yes, you forgot the backticks, those are needed: perl -e '$r=\ssh pass -p "AcN1234" ssh admin@172.20.20.12 sysserver`; print "Remote host:$r"' ` – terdon Jun 09 '15 at 11:23
  • @Oli absolutely, that's why my 1st solution was to use yours :). – terdon Jun 09 '15 at 11:24
  • i didnt miss that... the backticks were hidden by the webpage while pasting..!! i didnt miss that out..! :) – kiran bbnl Jun 09 '15 at 11:26
  • @kiranbbnl you added a space between ssh and pass. sshpass is a program, you need to use the exact name. – terdon Jun 09 '15 at 12:30