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

Updated:   |  Perl (Scripting) articles

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");


Did you find this article helpful?

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


Add a Comment

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