Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: | )

You can execute commands on remote machines from a Perl script using the Net::SSH::Perl module.

This module allows you to execute a command remotely and receive the STDOUT, STDERR, and exit status of that remote command.

One big advantage of Net::SSH::Perl over other methods is that you can automate the login process, that way you can write fully automated perl scripts, no console interaction is required in order to authenticate in the remote machine.

Example:

#!/usr/bin/perl
 
use Net::SSH::Perl;
 
my $host = "perlhowto.com";
my $user = "user";
my $password = "password";
 
#-- set up a new connection
my $ssh = Net::SSH::Perl->new($host);
#-- authenticate
$ssh->login($user, $pass);
#-- execute the command
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$user");

-- IMPORTANT NOTE!!

Execution of Net::SSH::Perl commands can be quite slow if you don't have the Math::BigInt::GMP module; so be sure that you have that module installed (or install it if you don't) in order to avoid the slowness problem.


execute commands on remote machines using ssh (ctrl-c)

Novice programmer here. Using the above topic example, how would one send "ctrl-c"?
would it be:

my($stdout, $stderr, $exit) = $ssh->cmd("\cC");

I'm trying to modify some existing perl code (which uses NET::SSH:PERL) but need a way to send ctrl-c to stop a previous sent command (after a sleep). What I tried above did not work for me.

thanks for any help!

port 22: No connection could be made because the target machine

use Net::SSH::Perl;

port 22: No connection could be made because the target machine actively refused

How to solve this error message ?
Can anyone answer this?

-------
Himanshu

It looks like the host you

It looks like the host you are connecting to is not listening to port 22, are you sure ssh is enabled in that host?

Can't connect to

Can't connect to, port 22: No connection could be made because the target machine actively refused it. at C:/strawberry/perl/lib/Net/SSH/Perl.pm line 20
iam getting this error when trying to execute this script.....
can anybody help me...

Net::Perl::SSH problem: Installation successful

Hi,

Am able to install Net::Perl::SSH on my windows desktop but when i try to connect to any host: unix or windows it runs into an infinite loop and never comes back to prompt. It gets connected to the host but doesn't return back.

Anybody out there who can help me please.!!

Thanks

Make sure that you don't

Make sure that you don't have in your windows computer any firewall blocking connections to/from port 22 (try to connect first using Putty or similar).

BTW, this question should be in the forum, post this kind of queestions here

update

Net::SSH2 and Net::OpenSSH are also available now