How to make an SSH connection to another server in Perl

Home > Search > How-to
  by

Download and install the Net::SSH::Perl module.

~]# cpan
~]# force install Net::SSH::Perl

 

Or install Net::SSH:Perl using this method.

curl -L http://cpanmin.us | perl - --sudo App::cpanminus
cpanm Net::SSH::Perl

 

Create an SSH connection to another host.

#!/usr/bin/perl

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

my $host = "your_hostname";
my $user = "your_username";
my $pass = "your_password";
my $cmd = "hostname";

my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
($stdout, $stderr, $exit) = $ssh->cmd($cmd);

chomp($stdout);
my $result = $stdout;
print $result;

 



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




Comments