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