1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
use Net::SSH2;
use strict;
use warnings;
use feature qw(:5.10);
use List::MoreUtils qw(pairwise);
my $ip="192.168.10.10";
my $password="password";
my $user="admin";
my $enable=$password;
my $ssh2 = Net::SSH2->new();
$ssh2->connect("$ip") or die;
if ($ssh2->auth_password("$user","$password")) {
my $chan2 = $ssh2->channel();
$chan2->shell();
$chan2->blocking(0);
print $chan2 "terminal length 0\n";
print $chan2 "enable\n";
print $chan2 "$enable\n";
print $chan2 "sh interfaces counters\n";
my %var;
our ($a, $b);
my @col_names;
while (defined( my $line = <$chan2>)) {
chomp $line;
if ($line =~ /^Port/) {
@col_names = split /\s+/, $line;
shift @col_names;
}
elsif (@col_names) {
if (my @col_values = split /\s+/, $line) {
my $key = shift @col_values;
say "Line: $line";
say "Key: $key";
say join ", ", pairwise { $a." => ".$b } @col_names, @col_values;
$var{$key} = { pairwise { $a => $b } @col_names, @col_values } if defined $key;
}
}
}
say "Key Gi0/2, InOctets: ", $var{"Gi0/2"}->{"InOctets"};
$chan2->close();
} else {
warn "auth error.\n";
} |
Partager