FreeKB - Net::SSH::Perl - Getting Started
Net::SSH::Perl - Getting Started

Home > Search


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

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 $ssh  = Net::SSH::Perl->new($host);

$ssh->login($user, $pass);
$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 617ad in the box below so that we can be sure you are a human.




Comments