read switch mac list and show names using it
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 4 Apr 2022 08:40:17 +0000 (10:40 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 4 Apr 2022 08:40:17 +0000 (10:40 +0200)
neighbors.pl

index 1ea5ff2..dcf7ce0 100755 (executable)
@@ -3,8 +3,24 @@ use warnings;
 use strict;
 use autodie;
 
+# ./sw-ip-name-mac.sh
 # ./sw-names | xargs -i ./dell-switch.pl {} 'show lldp neighbors'
 
+use Data::Dump qw(dump);
+
+my $mac2ip;
+my $mac2name;
+
+open(my $f, '<'. '/dev/shm/sw-ip-name-mac');
+while(<$f>) {
+       chomp;
+       my ( $ip, $name, $mac ) = split(/ /,$_);
+       $mac2ip->{$mac} = $ip;
+       $mac2name->{$mac} = $name;
+}
+
+warn "# mac2name = ",dump($mac2name);
+
 foreach my $file ( glob('log/*lldp*') ) {
        my ( undef, $name, undef ) = split(/_/, $file);
        #print "# $name $file\n";
@@ -28,13 +44,25 @@ foreach my $file ( glob('log/*lldp*') ) {
                        next;
                }
                if ( defined($line_regex) &&  /$line_regex/ ) {
+                       # Port       Device ID          Port ID          System Name     Capabilities 
                        my @v = ( $1, $2, $3, $4, $5 );
                        @v = map { s/^\s+//; s/\s+$//; $_ } @v;
-                       if ( length($v[1]) == 6 ) {
+
+                       if ( length($v[1]) == 6 ) { # decode text mac
                                $v[1] = unpack('H*', $v[1]);
                                $v[1] =~ s/(..)/$1:/g;
                                $v[1] =~ s/:$//;
                        }
+
+                       if ( exists $mac2name->{$v[1]} ) {
+                               my $mac_name = $mac2name->{$v[1]};
+                               if ( $v[3] eq '' ) {
+                                       $v[3] = $mac_name;
+                               } else {
+                                       warn "ERROR: name different $v[3] != $mac_name" if $v[3] ne $mac_name;
+                               }
+                       }
+
                        #my ( $port, $device_id, $port_id, $system_name, $cap ) = @v;
                        if ( $v[0] =~ m/^$/ ) {
                                my @old = @{ pop @ports };
@@ -51,6 +79,6 @@ foreach my $file ( glob('log/*lldp*') ) {
        }
 
        foreach my $p ( @ports ) {
-               print "$name ", join(' ', @$p ), "\n";
+               print "$name ", join(' ', @$p ), "\n";
        }
 }