Debian Codegraph @

test/ip-addr-to-if-index

raw
1 #!/usr/local/bin/perl -w
2 ## Name: test/ip-addr-to-if-index
3 ## Author: Simon Leinen <[email protected]>
4 ## Description: Find ifIndex for given IP address
5 ######################################################################
6 ## Usage: test/ip-addr-to-if-index [-q] community@hostname ipaddr...
7 ##
8 ## This script finds the interface indices corresponding to the given
9 ## IP addresses on the node.
10 ##
11 ## Example:
12 ##
13 ## $ ./test/ip-addr-to-if-index public@babar 127.0.0.1 130.59.4.2
14 ## 127.0.0.1 lo0 1
15 ## 130.59.4.2 hme0 2
16 ##
17 ## The `-q' form suppresses the IP addresses and interface
18 ## description:
19 ##
20 ## $ ./test/ip-addr-to-if-index -q public@babar 127.0.0.1 130.59.4.2
21 ## 1
22 ## 2
23 ######################################################################
24
25 require 5.002;
26 use strict;
27 use SNMP_Session;
28 use BER;
29
30 sub usage();
31 sub ether_hex($ );
32 sub ifDescr($ $ );
33
34 my ($hostname,$community,$quiet);
35 $quiet = 0;
36 while ($ARGV[0] =~ /^-/) {
37 if ($ARGV[0] eq '-q') {
38 $quiet = 1;
39 shift @ARGV;
40 } else {
41 usage ();
42 }
43 }
44 $hostname = shift @ARGV || usage();
45 ($community,$hostname) = ($hostname =~ /^(.*)@(.*)$/);
46
47 my $session;
48
49 die "Couldn't open SNMP session to $hostname"
50 unless ($session = SNMP_Session->open ($hostname, $community, 161));
51
52 my $ip_address;
53 while ($ip_address = shift @ARGV) {
54 my ($ip1,$ip2,$ip3,$ip4);
55 usage()
56 unless (($ip1,$ip2,$ip3,$ip4)
57 = $ip_address =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/);
58
59 my @ifIndex_OID = qw(1 3 6 1 2 1 4 20 1 2);
60 push @ifIndex_OID, $ip1;
61 push @ifIndex_OID, $ip2;
62 push @ifIndex_OID, $ip3;
63 push @ifIndex_OID, $ip4;
64
65 my @oids = (encode_oid (@ifIndex_OID));
66
67 if ($session->get_request_response (@oids)) {
68 my $response = $session->pdu_buffer;
69 my ($bindings, $binding, $oid, $value);
70 my ($ifIndex);
71
72 ($bindings) = $session->decode_get_response ($response);
73
74 ## IfIndex
75 ($binding,$bindings) = decode_sequence ($bindings);
76 ($oid,$value) = decode_by_template ($binding, "%O%@");
77 $ifIndex = pretty_print ($value);
78
79 if (!$quiet) {
80 printf STDOUT ("%-15s %-10s ", $ip_address,
81 &ifDescr ($ifIndex, $session));
82 }
83 printf STDOUT ("%d\n", $ifIndex);
84
85 } else {
86 die "No response received.\n";
87 }
88 }
89
90 $session->close ();
91
92 1;
93
94 sub usage ()
95 {
96 die "usage: $0 community\@hostname ipaddr...";
97 }
98
99 ## ether_hex (HEX_STRING)
100 ##
101 ## Converts a raw hex representation into the common form used in
102 ## Ethernet addresses, e.g. "080020830069" becomes
103 ## "08:00:20:83:00:69".
104 ##
105 sub ether_hex ($ )
106 {
107 my ($string) = @_;
108 $string =~ s/([0-9a-f][0-9a-f])/$1:/g;
109 $string =~ s/:$//;
110 $string;
111 }
112
113 my %ifDescrCache;
114
115 ## ifDescr (IFINDEX, SESSION)
116 ##
117 ## Return the interface description associated with the given
118 ## IFINDEX. Uses SESSION as the destination for SNMP request.
119 ## Results are cached in %ifDescrCache to avoid sending the same SNMP
120 ## request more than once.
121 ##
122 sub ifDescr($ $ )
123 {
124 my @ifDescr = split ('\.','1.3.6.1.2.1.2.2.1.2');
125 my ($ifIndex, $session) = @_;
126
127 return $ifDescrCache{$ifIndex,$session}
128 if defined ($ifDescrCache{$ifIndex,$session});
129 push @ifDescr,$ifIndex;
130 if ($session->get_request_response (encode_oid (@ifDescr))) {
131 my $response = $session->pdu_buffer;
132 my ($bindings, $binding, $oid, $value);
133
134 ($bindings) = $session->decode_get_response ($response);
135 ($binding,$bindings) = decode_sequence ($bindings);
136 ($oid,$value) = decode_by_template ($binding, "%O%@");
137 return $ifDescrCache{$ifIndex,$session} = pretty_print ($value);
138 } else {
139 return "if#".$ifIndex;
140 }
141 }