#!/usr/bin/perl use warnings; use strict; use Net::SSH2; # assuming a user named 'z' for demonstration # connecting to localhost, so you need your sshd running my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; $ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec('ls -la'); while (<$chan>){ print } # run a second command in $chan $chan->exec('dir'); while (<$chan>){ print } #will get dir named / thru another channel my $chan1 = $ssh2->channel(); $chan1->blocking(1); $chan1->exec('ls -la /'); while (<$chan1>){ print } #shell use my $chan2 = $ssh2->channel(); $chan2->blocking(1); $chan2->shell(); #opens shell on server print $chan2 "uname -a\n"; print "LINE1 : $_" while <$chan2>; # from perldoc Net::SSH2::Channel # Note that only one invocation of "process" or any of the shortcuts # "shell", "exec" or "subsystem" is allowed per channel. In order to run # several commands, shells or/and subsystems, a new "Channel" instance # must be used for every one. # only 1 command per shell, the next line won't run print $chan2 "who\n"; print "LINE2 : $_" while <$chan2>; $chan2->close; $ssh2->disconnect(); exit; __END__