| 1 |
#!/usr/local/bin/perl -w |
| 2 |
|
| 3 |
require 5.002; |
| 4 |
use strict; |
| 5 |
use SNMP_Session "0.59"; |
| 6 |
use BER; |
| 7 |
use Socket; |
| 8 |
|
| 9 |
sub usage (); |
| 10 |
|
| 11 |
my $bgpPeerState = [1,3,6,1,2,1,15,3,1,2]; |
| 12 |
my $bgpPeerRemoteAs = [1,3,6,1,2,1,15,3,1,9]; |
| 13 |
|
| 14 |
my $hostname = $ARGV[0] || usage (); |
| 15 |
my $community = $ARGV[1] || "public"; |
| 16 |
|
| 17 |
my $session; |
| 18 |
|
| 19 |
die "Couldn't open SNMP session to $hostname" |
| 20 |
unless ($session = SNMP_Session->open ($hostname, $community, 161)); |
| 21 |
$session->map_table ([$bgpPeerState, $bgpPeerRemoteAs], |
| 22 |
sub () { |
| 23 |
my ($index, $state, $as) = @_; |
| 24 |
grep (defined $_ && ($_=pretty_print $_), |
| 25 |
($state, $as)); |
| 26 |
my $pretty_state = (qw(? idle connect active |
| 27 |
opensent openconfirm |
| 28 |
established))[$state]; |
| 29 |
printf "%-15s %-12s %32s AS%-5s\n", |
| 30 |
$index, $pretty_state, |
| 31 |
hostname ($index), $as; |
| 32 |
}); |
| 33 |
$session->close (); |
| 34 |
|
| 35 |
1; |
| 36 |
|
| 37 |
sub pretty_addr ($ ) { |
| 38 |
my ($addr) = @_; |
| 39 |
my ($hostname,$aliases,$addrtype,$length,@addrs) |
| 40 |
= gethostbyaddr (inet_aton ($addr), AF_INET); |
| 41 |
$hostname ? $hostname." [".$addr."]" : $addr; |
| 42 |
} |
| 43 |
|
| 44 |
sub hostname ($ ) { |
| 45 |
my ($addr) = @_; |
| 46 |
my ($hostname,$aliases,$addrtype,$length,@addrs) |
| 47 |
= gethostbyaddr (inet_aton ($addr), AF_INET); |
| 48 |
$hostname || "[".$addr."]"; |
| 49 |
} |
| 50 |
|
| 51 |
sub usage () { |
| 52 |
die "usage: $0 host [community]"; |
| 53 |
} |