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

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.

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new('server1.example.com');

 

Data::Dumper could then be used to print the $ssh object.

use Data::Dumper;
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' );

 

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;

 

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.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |