How to ssh using perl scipt,run a command, and return the output...?

23,355

Solution 1

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

Solution 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"'
    
Share:
23,355

Related videos on Youtube

kiran bbnl
Author by

kiran bbnl

Updated on September 18, 2022

Comments

  • kiran bbnl
    kiran bbnl over 1 year

    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.

    • Oli
      Oli almost 9 years
      What didn't Googling "perl ssh" answer? It'll give you this example: stackoverflow.com/questions/2848725/… What more do you need?
    • kiran bbnl
      kiran bbnl almost 9 years
      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....!
  • terdon
    terdon almost 9 years
    @kiranbbnl yes, see updated answer.
  • Oli
    Oli almost 9 years
    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.
  • kiran bbnl
    kiran bbnl almost 9 years
    @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..? –
  • terdon
    terdon almost 9 years
    @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
    terdon almost 9 years
    @Oli absolutely, that's why my 1st solution was to use yours :).
  • kiran bbnl
    kiran bbnl almost 9 years
    i didnt miss that... the backticks were hidden by the webpage while pasting..!! i didnt miss that out..! :)
  • terdon
    terdon almost 9 years
    @kiranbbnl you added a space between ssh and pass. sshpass is a program, you need to use the exact name.