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

by
Jeremy Canfield |
Updated: March 09 2020
| 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.
#!/usr/bin/perl
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