$ ssh 10.10.8.128
Copyright (c) 2002 - 2012 Juniper Networks, Inc. All rights reserved.
Username: admin
Password:
MX200-1> enable
Enter password:
MX200-1#
####
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use Net::SSH2;
my %hosts = (
"MX200-1" => "10.10.8.128"
);
my $DEBUG = 1;
foreach my $name (sort keys %hosts) {
my $ip = $hosts{$name};
$DEBUG && print "check $name ($ip)\n";
my $ssh2 = Net::SSH2->new();
if ($ssh2->connect($ip)) {
$DEBUG && print "connected\n";
$ssh2->debug($DEBUG);
$DEBUG && print "version=".join(', ', $ssh2->version())."\n";
$DEBUG && print "error=".$ssh2->error()."\n";
$ssh2->auth_list(); # Returns undef, auth_ok() will return 0 unless we call this(?)
if ($ssh2->auth_ok()) { # Returns 4 (= true, we have been authenticated OK)
$DEBUG && print "authenticated ok\n";
if (my $chan = $ssh2->channel()) {
$DEBUG && print "channel open\n";
foreach my $line (get_channel_lines($chan)) {
print $line."\n";
}
$DEBUG && print "done\n";
} else {
croak "Error opening channel";
}
} else {
croak "Expected to be authenticated: ".$ssh2->auth_ok()."\n";
}
} else {
warn "Error connecting to $name ($ip); please check host status\n";
}
}
sub get_channel_lines {
my $chan = shift;
my $output = "";
until ($chan->eof) {
my $bytes = $chan->read(my $buffer, 1024); # Blocks
if (defined $bytes) {
$DEBUG && print "$buffer($bytes)\n";
$output .= $buffer;
}
}
return split(/\n/, $output);
}
####
check MX200-1 (10.10.8.128)
connected
version=1.2.6, 66054, SSH-2.0-libssh2_1.2.6
error=0
authenticated ok
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type, window_size, packet_size, ((void *)0) , 0 ) -> 0x9d08468
channel open
Net::SSH2::Channel::read(size = 1024, ext = 0)