File: mdebug

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 (142 lines) | stat: -rwxr-xr-x 4,099 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/local/bin/perl -w

use strict;

use SNMP_Session;
use BER;
use Socket;

sub print_mroutes ($$);
sub pretty_next_hop_state ($);

my $version = '2c';
my $port = 161;
my $debug = 0;

while (defined $ARGV[0] && $ARGV[0] =~ /^-/) {
    if ($ARGV[0] =~ /^-v/) {
	if ($ARGV[0] eq '-v') {
	    shift @ARGV;
	    usage (1) unless defined $ARGV[0];
	} else {
	    $ARGV[0] = substr($ARGV[0], 2);
	}
	if ($ARGV[0] eq '1') {
	    $version = '1';
	} elsif ($ARGV[0] eq '2c') {
	    $version = '2c';
	} else {
	    usage (1);
	}
    } elsif ($ARGV[0] =~ /^-p/) {
	if ($ARGV[0] eq '-p') {
	    shift @ARGV;
	    usage (1) unless defined $ARGV[0];
	} else {
	    $ARGV[0] = substr($ARGV[0], 2);
	}
	if ($ARGV[0] =~ /^[0-9]+$/) {
	    $port = $ARGV[0];
	} else {
	    usage (1);
	}
    } elsif ($ARGV[0] eq '-h') {
	usage (0);
	exit 0;
    } else {
	usage (1);
    }
    shift @ARGV;
}
my $host = shift @ARGV || usage (1);
my $community = shift @ARGV || "public";
my ($group_name, $group, $group_quad);
my ($source_name, $source, $source_quad);
$group_name = shift @ARGV || die ("no source/group given");
if ($#ARGV >= $[) {
    $source_name = $group_name;
    $group_name = shift @ARGV;
}
usage (1) if $#ARGV >= $[;
if (! defined ($group = inet_aton ($group_name))) {
    die ("Cannot parse group $group_name");
}
$group_quad = inet_ntoa ($group);
if (! defined ($source = inet_aton ($source_name))) {
    die ("Cannot parse source $source_name");
}
$source_quad = inet_ntoa ($source);

my $session =
    ($version eq '1' ? SNMPv1_Session->open ($host, $community, $port)
     : $version eq '2c' ? SNMPv2c_Session->open ($host, $community, $port)
     : die "Unknown SNMP version $version")
  || die "Opening SNMP_Session";
print_mroutes ($session, $group);
1;

sub print_mroutes ($$) {
    my $ipMRouteUpstreamNeighbor = [1,3,6,1,3,60,1,1,2,1,4];
    my $ipMRouteInIfIndex = [1,3,6,1,3,60,1,1,2,1,5];
    my $ipMRouteUpTime = [1,3,6,1,3,60,1,1,2,1,6];
    my $ipMRouteExpiryTime = [1,3,6,1,3,60,1,1,2,1,7];
    my $ipMRoutePkts = [1,3,6,1,3,60,1,1,2,1,8];
    my $ipMRouteDifferentInIfPackets = [1,3,6,1,3,60,1,1,2,1,9];
    my $ipMRouteOctets = [1,3,6,1,3,60,1,1,2,1,10];
    my $ipMRouteProtocol = [1,3,6,1,3,60,1,1,2,1,11];

    my $ipMRouteNextHopState = [1,3,6,1,3,60,1,1,3,1,6];
    my $ipMRouteNextHopProtocol = [1,3,6,1,3,60,1,1,3,1,10];
    my ($session, $group) = @_;
    my @group_subids = split (/\./, inet_ntoa ($group), 4);
    my @oids = ([@{$ipMRouteUpstreamNeighbor},@group_subids],
		[@{$ipMRouteProtocol},@group_subids]);
    $session->map_table
	(\@oids,
	 sub () {
	     my ($index, $nbr, $protocol) = @_;
	     my ($source, $mask) = 
		 ($index =~ m/^(\d+\.\d+\.\d+\.\d+)\.(\d+\.\d+\.\d+\.\d+)$/);
	     map { $_ = pretty_print $_ if defined $_ }
	     ($nbr, $protocol);
	     print STDOUT ("$source ($mask) ",inet_ntoa ($group),": ",
			   pretty_next_hop_protocol ($protocol),
			   "\n");
	 });

    @oids = ([@{$ipMRouteNextHopState},@group_subids],
		[@{$ipMRouteNextHopProtocol},@group_subids]);
    $session->map_table
	(\@oids,
	 sub () {
	     my ($index, $state, $protocol) = @_;
	     my ($source, $mask, $if_index, $nexthop) = 
		 ($index =~ m/^(\d+\.\d+\.\d+\.\d+)\.(\d+\.\d+\.\d+\.\d+)\.(\d+)\.(\d+\.\d+\.\d+\.\d+)$/);
	     map { $_ = pretty_print $_ if defined $_ }
	     ($state, $protocol);
	     print STDOUT ("$source ($mask) ",inet_ntoa ($group),": ",
			   pretty_next_hop_state ($state)," ",
			   pretty_next_hop_protocol ($protocol),
			   "\n");
	 });
}

sub pretty_next_hop_state ($ ) {
    return "forwarding(1)" if $_[0] eq 1;
    return "pruned(2)" if $_[0] eq 2;
    return "???($_[0])";
}

sub pretty_next_hop_protocol ($ ) {
    return "other(1)" if $_[0] eq 1;
    return "local(2)" if $_[0] eq 2;
    return "netmgmt(3)" if $_[0] eq 3;
    return "dvmrp(4)" if $_[0] eq 4;
    return "mospf(5)" if $_[0] eq 5;
    return "pimSparseDense(6)" if $_[0] eq 6;
    return "cbt(7)" if $_[0] eq 7;
    return "pimSparseMode(8)" if $_[0] eq 8;
    return "pimDenseMode(9)" if $_[0] eq 9;
    return "igmpOnly(10)" if $_[0] eq 10;
    return "???($_[0])";
}