- $ssh->auth_password('username','password') or $ssh->die_with_error;
- $ssh->auth(username => 'username', password => 'password') or $ssh->die_with_error;
!/usr/bin/perl
use Net::SSH2;
$host = "192.168.254.10";
$user = "root";
$pass = "default";
my $ssh2 = Net::SSH2->new();
$ssh2->debug(1);
$ssh2->connect($host) or die $!;
$ssh2->auth(username => $user, password => $pass) or die "Unable to login \n".$ssh2->die_with_error;
print "Connected to '$host' as '$user' \n";
my $chan = $ssh2->channel();
$chan->exec('uptime');
Read remote controller program header
read_chan($chan);
close channel and disconnect
$chan->close;
$ssh2->disconnect;
read_chan: read all input from chan, dump it to STDOUT
sub read_chan {
my ($chan) = @_;
we switch to non-blocking and sleep between retries because it
would fail to read anything with a blocking call... Still trying
to find out why the blocking call would not read nothing.
$chan->blocking(0);
my $done;
while (1) {
my $in;
while (my $bytes = $chan->read($in, 1024)) {
$in =~ s/^/> /gm;
print $in;
$done++;
}
We leave after one sucessful read, this could be improved...
last if $done;
select(undef, undef, undef, .25); sleep 50 ms
}
$chan->blocking(1);
}
Output:
root@Linux200:~ perl connect.pl
Connected to '192.168.254.10' as 'root'
> 16:24:56 up 6:37, 1 user, load average: 0.00, 0.01, 0.00
Debian / perl 5.24.1