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

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

Here is a really simple example to get started with Net::OpenSSH.


use strict;
use warnings;
use Net::OpenSSH;

my $host  = "your_hostname";
my $ssh   = Net::OpenSSH->new($host);
$ssh->system("ls /tmp");


Or, you could store the results in a variable and then print the variable.

my ($out) = $ssh->capture2("ls /tmp");
chomp $out;
print "$out\n";


Username / Password

With the above usage, you will be prompted for the users password when connecting to the target SSH server. To include the password in the Perl script, installed the IO::Pty module and then use the following.

my $user  = "your_username";
my $pass  = "your_password";
my $host  = "your_hostname";
my $ssh   = Net::OpenSSH->new("$user:$pass\@$host");


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 92e4d in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |