X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=show-switch;h=9dab19c7317dc074e0c2db19c9adf85d630a7b80;hb=HEAD;hp=0e23fdcdd26f720dbfa7681ca37902bcfcf9c58f;hpb=9e2b06aae8530bfe4ea28e1cfcc5bf8d1598cdb4;p=jablonka.git diff --git a/show-switch b/show-switch index 0e23fdc..9dab19c 100755 --- a/show-switch +++ b/show-switch @@ -8,9 +8,11 @@ use Data::Dumper; use Getopt::Long; use List::Util; +no warnings 'uninitialized'; + sub usage { die <] +Usage: $0 [] Options: --debug Show debugging outputs @@ -90,8 +92,12 @@ my $basics = my_get_table({ 'name' => "$OID_basic.5", 'location' => "$OID_basic.6", }); -print Dumper($basics) if $debug; +print "# Basics:\n", Dumper($basics) if $debug; my $bas = $basics->{0} or die "Cannot find basic info"; +for (values %$bas) { + s{\r}{}gs; + s{\n}{ | }gs; +} print "### Basics ###\n\n"; print "Device: ", $bas->{desc}, "\n"; @@ -117,8 +123,12 @@ my %if_macs = (); for my $if (values %$if_table) { $if->{mac} = join(':', map { sprintf "%02x", ord $_ } split(//, $if->{mac})); $if_macs{$if->{mac}} = 1; + if ($if->{hispeed} == $if->{speed}) { + # Some buggy switches put hispeed == speed + delete $if->{hispeed}; + } } -print Dumper($if_table) if $debug; +print "# Interface table:\n", Dumper($if_table) if $debug; my @ifaces = sort { $a <=> $b } keys %$if_table; print "MAC addr: ", join(" ", sort keys %if_macs), "\n"; @@ -127,7 +137,7 @@ my $ip_table = my_get_table({ 'iface' => "$OID_ipTable.1.2", 'mask' => "$OID_ipTable.1.3", }); -print Dumper($ip_table) if $debug; +print "# IP table:\n", Dumper($ip_table) if $debug; # XXX: IPv6 not supported yet for my $ipa (keys %$ip_table) { @@ -136,7 +146,20 @@ for my $ipa (keys %$ip_table) { my $nm = Net::Netmask->new2($ipa, $ip->{mask}) or die "Cannot parse IP prefix"; push @{$if->{ip_addrs}}, $ipa . '/' . $nm->bits; } -print Dumper($if_table) if $debug; + +# Switch ports +my $OID_1dBasePortTable = '1.3.6.1.2.1.17.1.4'; +my $port_table = my_get_table({ + 'ifindex' => "$OID_1dBasePortTable.1.2", +}); +print "# Switch ports:\n", Dumper($port_table) if $debug; + +for my $port (keys %$port_table) { + my $if = $if_table->{$port_table->{$port}->{ifindex}} or die "Port table refers to unknown iface"; + $if->{switch_port} = $port; +} + +print "# Extended interface table:\n", Dumper($if_table) if $debug; if ($mac) { my $OID_1qTpFdbTable = '1.3.6.1.2.1.17.7.1.2.2'; @@ -144,17 +167,18 @@ if ($mac) { 'port' => "$OID_1qTpFdbTable.1.2", 'status' => "$OID_1qTpFdbTable.1.3", }); - print Dumper($vlan_fdb_table) if $debug; + print "# VLAN FDB:\n", Dumper($vlan_fdb_table) if $debug; if (%$vlan_fdb_table) { for my $m (keys %$vlan_fdb_table) { my $fdb = $vlan_fdb_table->{$m}; $fdb->{status} == 3 or next; # Only learned MACs - my $port = $if_table->{$fdb->{port}} or die "Forwarding DB refers to unknown iface"; + my $port = $port_table->{$fdb->{port}} or die "Forwarding DB refers to unknown port"; + my $if = $if_table->{$port->{ifindex}} or die "Forwarding DB refers to unknown iface"; my @m = split /\./, $m; my $vlan = shift @m; my $mac = join(':', map { sprintf('%02x', $_) } @m); - push @{$port->{macs}}, $mac; + push @{$if->{macs}}, $mac; } } else { print "# Trying fall-fack to .1d FDB\n" if $debug; @@ -163,14 +187,15 @@ if ($mac) { 'port' => "$OID_1dTpFdbTable.1.2", 'status' => "$OID_1dTpFdbTable.1.3", }); - print Dumper($fdb_table) if $debug; + print "# Non-VLAN FDB:\n", Dumper($fdb_table) if $debug; for my $m (keys %$fdb_table) { my $fdb = $fdb_table->{$m}; $fdb->{status} == 3 or next; # Only learned MACs - my $port = $if_table->{$fdb->{port}} or die "Forwarding DB refers to unknown iface"; + my $port = $port_table->{$fdb->{port}} or die "Forwarding DB refers to unknown port"; + my $if = $if_table->{$port->{ifindex}} or die "Forwarding DB refers to unknown iface"; my @m = split /\./, $m; my $mac = join(':', map { sprintf('%02x', $_) } @m); - push @{$port->{macs}}, $mac; + push @{$if->{macs}}, $mac; } } @@ -192,7 +217,7 @@ for my $vlan (values %$vlan_table) { $vlan->{$k} = [ split //, unpack("B*", $vlan->{$k} // "") ]; } } -print Dumper($vlan_table) if $debug; +print "# VLAN table\n", Dumper($vlan_table) if $debug; my @vlans = sort { $a <=> $b } grep { $vlan_table->{$_}->{'row-status'} == 1 } keys %$vlan_table; print "\n### VLANs ###\n\n"; @@ -205,9 +230,9 @@ if (@vlans) { } print "\n### Ports ###\n\n"; -# XXX: We assume that 802.1d switch ports IDs are equal to interface IDs -for my $port (@ifaces) { - my $if = $if_table->{$port}; +for my $ifindex (@ifaces) { + my $if = $if_table->{$ifindex}; + my $port = $if->{switch_port}; my $state; my $scolor = $t_norm; if ($if->{'admin'} != 1) { @@ -228,7 +253,7 @@ for my $port (@ifaces) { $speed = int($speed/1000); $speed = "${speed}G"; } - printf "%-4d %-15.15s %s%-4s %-5s %s%-20s%s", $port, $if->{name}, $scolor, $state, $speed, $t_yellow, $if->{alias}, $t_norm; + printf "%-4d %-15.15s %s%-4s %-5s %s%-25.25s%s", $ifindex, $if->{name}, $scolor, $state, $speed, $t_yellow, $if->{alias}, $t_norm; if ($mac) { my $show_mac = ""; @@ -238,7 +263,7 @@ for my $port (@ifaces) { $show_mac = $macs[0]; $more_macs = "${t_yellow}+${t_norm}" if @macs > 1; } - printf "%-17s%s ", $show_mac, $more_macs; + printf " %-17s%s ", $show_mac, $more_macs; } for my $vid (@vlans) {