How to ssh using perl scipt,run a command, and return the output...?
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:
- Do it cleanly as described in Oli's answer.
- Set up passwordless, key-based ssh authentication.
-
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"'
Related videos on Youtube
kiran bbnl
Updated on September 18, 2022Comments
-
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 almost 9 yearsWhat didn't Googling "perl ssh" answer? It'll give you this example: stackoverflow.com/questions/2848725/… What more do you need?
-
kiran bbnl almost 9 yearsI 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 almost 9 years@kiranbbnl yes, see updated answer.
-
Oli almost 9 yearsNet::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 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 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 almost 9 years@Oli absolutely, that's why my 1st solution was to use yours :).
-
kiran bbnl almost 9 yearsi didnt miss that... the backticks were hidden by the webpage while pasting..!! i didnt miss that out..! :)
-
terdon almost 9 years@kiranbbnl you added a space between ssh and pass.
sshpass
is a program, you need to use the exact name.