Bootstrap FreeKB - Perl (Scripting) - Getting Started with Net::SSH::Perl
Perl (Scripting) - Getting Started with Net::SSH::Perl

Updated:   |  Perl (Scripting) articles

This assumes you have installed the Net::SSH::Perl module on your system.

At a bare minimum, the following is all that is needed to create the $ssh object. Data::Dumper is used to print the $ssh object.

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

my $ssh = Net::SSH::Perl->new('server1.example.com');
print Dumper $ssh;

 

Which should return something like this.

$VAR1 = bless( {
                 'client_version_string' => 'SSH-2.0-2.01',
                 '_test' => undef,
                 'host' => 'server1.example.com',
                 'use_protocol' => 2,
                 'server_version_string' => 'SSH-2.0-OpenSSH_7.4',
                 'datafellows' => 0,
                 'session' => {
                                'sock' => \*Symbol::GEN0
                              },
                 'config' => bless( {
                                      'o' => {
                                               'protocol' => 7,
                                               'password_prompt_host' => 1,
                                               'password_prompt_login' => 1,
                                               'global_known_hosts' => '/etc/ssh_known_hosts2',
                                               'user_known_hosts' => '/home/john.doe/.ssh/known_hosts',
                                               'auth_kbd_interactive' => 1,
                                               'auth_dsa' => 1,
                                               'identity_files' => [
                                                                     '/home/john.doe/.ssh/id_dsa'
                                                                   ],
                                               'auth_password' => 1,
                                               'number_of_password_prompts' => 3
                                             },
                                      'host' => 'server1.example.com'
                                    }, 'Net::SSH::Perl::Config' )
               }, 'Net::SSH::Perl::SSH2' );

 

And here is how you can set parameters.

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

my %ssh_params = ( protocol => "2,1",
                   identity_files => ["/home/john.doe/.ssh/id_ecdsa"],
                   options => [    "BatchMode yes",
                                   "ConnectTimeout 3",
                                   "StrictHostKeyChecking no"],
                   debug => 'true'
        );

my $ssh = Net::SSH::Perl->new('server1.example.com', %ssh_params);
print Dumper $ssh;

 

Next, you'll attempt to log into the host. Instead of using a password, you could use a key pair for authentication.

$ssh->login('john.doe', 'itsasecret');

 

Again, Dumper could then be used to print the $ssh object. The $ssh object should contain much more data now.

use Data::Dumper;
print Dumper $ssh;

 

Be aware that the Dumper output will contain the cleartext password if password authentication was used.

'config' => bless( {
                     'o' => {
                              'pass' => 'itsasecret',

 

Now you can try a command.

$ssh->cmd("type the commands you want to run on the remote host here");

 

The eval function can be used to determine if the SSH connection to the host is successful, or if there is some issue.

#!/usr/bin/perl

use strict;
use warnings;
use Net::SSH::Perl;

my $host = "your_hostname";
my $user = "your_username";
my $pass = "your_password";
my $ssh  = "";

eval {
  $ssh = Net::SSH::Perl->new($host);
  $ssh->login($user, $pass);
};

chomp $@;

if ($@) {
  print "eval found the following issue: $@ \n";
}
else {
  $ssh->cmd("type the commands you want to run on the remote host here");
}

 

Running the above script will execute the commands you want to run on the remote host. However, the result of the command will not be accessible in Perl. If the command you are running on the remote host returns values, use the following so that Perl can print the values returned by the command.

my ($stdout, $stderr, $exit) = $ssh->cmd("type the commands you want to run on the remote host here");

chomp $stdout;
chomp $stderr;
chomp $exit;

print "Standard Out   = $stdout\n";
print "Standard Error = $stderr\n";
print "Exit           = $exit\n";

 

Running the script will produce the result of the command you ran on the remote host. For example, if you ran the "hostname" command on the remote host, the result would be the hostname.

name.example.com

 

When getting data from another server over SSH, the data will be stored in a variable. Split can be used to convert the variable into an array.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 51dce4 in the box below so that we can be sure you are a human.