#!/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); }