11 die "Usage: $0 [--debug] [--mac] <ip-addr>\n";
22 my ($switch_ip) = @ARGV;
23 my $community = 'public';
25 my $is_tty = -t STDOUT;
28 return $is_tty ? `tput $a` : "";
30 my $t_red = attr("setaf 1");
31 my $t_green = attr("setaf 2");
32 my $t_yellow = attr("setaf 3");
33 my $t_norm = attr("sgr0");
35 my ($snmp, $err) = Net::SNMP->session(
36 -hostname => $switch_ip,
38 -community => $community,
40 $snmp or die "Cannot establish session: $err\n";
46 for my $c (keys %$cols) {
47 my $depth = split /\./, $cols->{$c};
48 my $t = $snmp->get_table(-baseoid => $cols->{$c}) or next;
49 for my $k (keys %$t) {
50 my @k = split /\./, $k;
51 my $kk = join('.', @k[$depth..$#k]);
52 $tab->{$kk}->{$c} = $t->{$k};
63 $d = int($t/86400) . " days, ";
66 return $d . sprintf "%02d:%02d:%02d", int($t/3600), int(($t%3600)/60), $t%60;
69 my $OID_basic = '1.3.6.1.2.1.1';
71 my $basics = my_get_table({
72 'desc' => "$OID_basic.1",
73 'uptime' => "$OID_basic.3",
74 'contact' => "$OID_basic.4",
75 'name' => "$OID_basic.5",
76 'location' => "$OID_basic.6",
78 print Dumper($basics) if $debug;
79 my $bas = $basics->{0} or die "Cannot find basic info";
81 print "### Basics ###\n\n";
82 print "Device: ", $bas->{desc}, "\n";
83 print "Uptime: ", format_uptime($bas->{uptime}), "\n";
84 print "Contact: ", $bas->{contact}, "\n";
85 print "Name: ", $bas->{name}, "\n";
86 print "Location: ", $bas->{location}, "\n";
88 my $OID_ifTable = '1.3.6.1.2.1.2.2';
89 my $OID_ifTablev2 = '1.3.6.1.2.1.31.1.1';
91 my $if_table = my_get_table({
92 'desc' => "$OID_ifTable.1.2",
93 'name' => "$OID_ifTablev2.1.1",
94 'alias' => "$OID_ifTablev2.1.18",
95 'speed' => "$OID_ifTable.1.5",
96 'hispeed' => "$OID_ifTablev2.1.15",
97 'admin' => "$OID_ifTable.1.7",
98 'oper' => "$OID_ifTable.1.8",
100 print Dumper($if_table) if $debug;
101 my @ifaces = sort { $a <=> $b } keys %$if_table;
103 my $OID_ipTable = '1.3.6.1.2.1.4.20';
104 my $ip_table = my_get_table({
105 'iface' => "$OID_ipTable.1.2",
106 'mask' => "$OID_ipTable.1.3",
108 print Dumper($ip_table) if $debug;
110 # XXX: IPv6 not supported yet
111 for my $ipa (keys %$ip_table) {
112 my $ip = $ip_table->{$ipa};
113 my $if = $if_table->{$ip->{iface}} or die "IP table refers to unknown iface";
114 my $nm = Net::Netmask->new2($ipa, $ip->{mask}) or die "Cannot parse IP prefix";
115 push @{$if->{ip_addrs}}, $ipa . '/' . $nm->bits;
117 print Dumper($if_table) if $debug;
120 my $OID_1qTpFdbTable = '1.3.6.1.2.1.17.7.1.2.2';
121 my $OID_1qPort = "$OID_1qTpFdbTable.1.2";
122 my $OID_1qStatus = "$OID_1qTpFdbTable.1.3";
123 my $vlan_fdb_table = my_get_table({
124 'port' => $OID_1qPort,
125 'status' => $OID_1qStatus,
127 print Dumper($vlan_fdb_table) if $debug;
129 for my $m (keys %$vlan_fdb_table) {
130 my $fdb = $vlan_fdb_table->{$m};
131 my $port = $if_table->{$fdb->{port}} or die "Forwarding DB refers to unknown iface";
132 my @m = split /\./, $m;
134 my $mac = join(':', map { sprintf('%02x', $_) } @m);
135 push @{$port->{macs}}, $mac;
139 my $OID_VlanStaticTable = '1.3.6.1.2.1.17.7.1.4.3';
140 my $vlan_table = my_get_table({
141 'name' => "$OID_VlanStaticTable.1.1",
142 'egress-ports' => "$OID_VlanStaticTable.1.2",
143 'untagged-ports' => "$OID_VlanStaticTable.1.4",
144 'row-status' => "$OID_VlanStaticTable.1.5",
146 for my $vlan (values %$vlan_table) {
147 for my $k ('egress-ports', 'untagged-ports') {
148 $vlan->{$k} = [ split //, unpack("B*", $vlan->{$k} // "") ];
151 print Dumper($vlan_table) if $debug;
152 my @vlans = sort { $a <=> $b } grep { $vlan_table->{$_}->{'row-status'} == 1 } keys %$vlan_table;
154 print "\n### VLANs ###\n\n";
156 for my $vid (@vlans) {
157 printf "%-4d %s\n", $vid, $vlan_table->{$vid}->{'name'} // '-';
160 print "No VLAN support.\n";
163 print "\n### Ports ###\n\n";
164 # XXX: We assume that 802.1d switch ports IDs are equal to interface IDs
165 for my $port (@ifaces) {
166 my $if = $if_table->{$port};
168 my $scolor = $t_norm;
169 if ($if->{'admin'} != 1) {
171 } elsif ($if->{'oper'} != 1) {
178 my $speed = $if->{hispeed} || int($if->{speed} / 1000000);
181 } elsif ($speed < 1000) {
182 $speed = "${speed}M";
184 $speed = int($speed/1000);
185 $speed = "${speed}G";
187 printf "%-4d %-15.15s %s%-4s %-5s %s%-20s%s", $port, $if->{name}, $scolor, $state, $speed, $t_yellow, $if->{alias}, $t_norm;
190 my $macs = $if->{macs};
193 if ($macs && @$macs) {
194 $show_mac = $macs->[0];
195 $more_macs = "${t_yellow}+${t_norm}" if @$macs > 1;
197 printf "%-17s%s ", $show_mac, $more_macs;
200 for my $vid (@vlans) {
201 my $vlan = $vlan_table->{$vid};
202 if ($vlan->{'egress-ports'}->[$port]) {
203 if ($vlan->{'untagged-ports'}->[$port]) {
204 if ($t_green ne "") {
205 print " ${t_green}${vid}${t_norm}";
217 if ($if->{ip_addrs}) {
218 print "${t_yellow} IP: ", join(" ", @{$if->{ip_addrs}}), "${t_norm}\n";