From: Martin Mares Date: Sun, 28 Jan 2018 20:55:38 +0000 (+0100) Subject: show-switch: --allmac mode X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=3debedd4d95fcb19a957f361346f9d637f146204;p=jablonka.git show-switch: --allmac mode --- diff --git a/show-switch b/show-switch index 60e166f..1033759 100755 --- a/show-switch +++ b/show-switch @@ -6,21 +6,25 @@ use Net::SNMP (); use Net::Netmask; use Data::Dumper; use Getopt::Long; +use List::Util; sub usage { - die "Usage: $0 [--debug] [--mac] \n"; + die "Usage: $0 [--debug] [--mac] [--allmac] \n"; } my $debug = 0; my $mac = 0; +my $all_mac = 0; GetOptions( 'debug' => \$debug, 'mac' => \$mac, + 'allmac' => \$all_mac, ) or usage; @ARGV == 1 or usage; my ($switch_ip) = @ARGV; my $community = 'public'; +$mac ||= $all_mac; my $is_tty = -t STDOUT; sub attr { @@ -30,6 +34,7 @@ sub attr { my $t_red = attr("setaf 1"); my $t_green = attr("setaf 2"); my $t_yellow = attr("setaf 3"); +my $t_magenta = attr("setaf 5"); my $t_norm = attr("sgr0"); my ($snmp, $err) = Net::SNMP->session( @@ -90,15 +95,22 @@ my $OID_ifTablev2 = '1.3.6.1.2.1.31.1.1'; my $if_table = my_get_table({ 'desc' => "$OID_ifTable.1.2", - 'name' => "$OID_ifTablev2.1.1", - 'alias' => "$OID_ifTablev2.1.18", 'speed' => "$OID_ifTable.1.5", - 'hispeed' => "$OID_ifTablev2.1.15", + 'mac' => "$OID_ifTable.1.6", 'admin' => "$OID_ifTable.1.7", 'oper' => "$OID_ifTable.1.8", + 'name' => "$OID_ifTablev2.1.1", + 'hispeed' => "$OID_ifTablev2.1.15", + 'alias' => "$OID_ifTablev2.1.18", }); +my %if_macs = (); +for my $if (values %$if_table) { + $if->{mac} = join(':', map { sprintf "%02x", ord $_ } split(//, $if->{mac})); + $if_macs{$if->{mac}} = 1; +} print Dumper($if_table) if $debug; my @ifaces = sort { $a <=> $b } keys %$if_table; +print "MAC addr: ", join(" ", sort keys %if_macs), "\n"; my $OID_ipTable = '1.3.6.1.2.1.4.20'; my $ip_table = my_get_table({ @@ -128,12 +140,17 @@ if ($mac) { 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 @m = split /\./, $m; my $vlan = shift @m; my $mac = join(':', map { sprintf('%02x', $_) } @m); push @{$port->{macs}}, $mac; } + for my $if (values %$if_table) { + $if->{macs} or next; + $if->{macs} = [ sort(List::Util::uniq(@{$if->{macs}})) ]; + } } my $OID_VlanStaticTable = '1.3.6.1.2.1.17.7.1.4.3'; @@ -187,12 +204,12 @@ for my $port (@ifaces) { printf "%-4d %-15.15s %s%-4s %-5s %s%-20s%s", $port, $if->{name}, $scolor, $state, $speed, $t_yellow, $if->{alias}, $t_norm; if ($mac) { - my $macs = $if->{macs}; my $show_mac = ""; my $more_macs = " "; - if ($macs && @$macs) { - $show_mac = $macs->[0]; - $more_macs = "${t_yellow}+${t_norm}" if @$macs > 1; + my @macs = @{$if->{macs} // []}; + if (@macs) { + $show_mac = $macs[0]; + $more_macs = "${t_yellow}+${t_norm}" if @macs > 1; } printf "%-17s%s ", $show_mac, $more_macs; } @@ -217,4 +234,14 @@ for my $port (@ifaces) { if ($if->{ip_addrs}) { print "${t_yellow} IP: ", join(" ", @{$if->{ip_addrs}}), "${t_norm}\n"; } + if ($all_mac) { + if ($if->{mac}) { + print "${t_yellow} Local MAC: ", $if->{mac}, "${t_norm}\n"; + } + if ($if->{macs}) { + for my $m (@{$if->{macs}}) { + print "${t_magenta} MAC: $m${t_norm}\n"; + } + } + } }