File: list-ospf-neighbors

package info (click to toggle)
libsnmp-session-perl 1.14~git20221124T101957-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,104 kB
  • sloc: perl: 11,920; ansic: 25; makefile: 15
file content (47 lines) | stat: -rwxr-xr-x 1,195 bytes parent folder | download | duplicates (4)
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
#!/usr/local/bin/perl -w

require 5.002;
use strict;
use SNMP_Session "0.59";
use BER;
use Socket;

sub usage ();

my $ospfNbrIpAddr = [1,3,6,1,2,1,14,10,1,1];
my $ospfNbrRtrId = [1,3,6,1,2,1,14,10,1,3];
my $ospfNbrState = [1,3,6,1,2,1,14,10,1,6];

my $hostname = $ARGV[0] || usage ();
my $community = $ARGV[1] || "public";

my $session;

die "Couldn't open SNMP session to $hostname"
    unless ($session = SNMP_Session->open ($hostname, $community, 161));
$session->map_table ([$ospfNbrIpAddr, $ospfNbrRtrId, $ospfNbrState],
		     sub () {
			 my ($index, $addr, $router_id, $state) = @_;
			 grep (defined $_ && ($_=pretty_print $_),
			       ($addr, $router_id, $state));
			 my $pretty_state = (qw(? down attempt init twoWay
						exchangeStart exchange
						loading full))[$state];
			 print STDOUT (pretty_addr ($addr)," ",
				       pretty_addr ($router_id),
				       " (",$pretty_state,")\n");
		     });
$session->close ();

1;

sub pretty_addr ($ ) {
    my ($addr) = @_;
    my ($hostname,$aliases,$addrtype,$length,@addrs)
	= gethostbyaddr (inet_aton ($addr), AF_INET);
    $hostname ? $hostname." [".$addr."]" : $addr;
}

sub usage () {
  die "usage: $0 host [community]";
}