read switch mac list and show names using it
[dell-switch] / neighbors.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 # ./sw-ip-name-mac.sh
7 # ./sw-names | xargs -i ./dell-switch.pl {} 'show lldp neighbors'
8
9 use Data::Dump qw(dump);
10
11 my $mac2ip;
12 my $mac2name;
13
14 open(my $f, '<'. '/dev/shm/sw-ip-name-mac');
15 while(<$f>) {
16         chomp;
17         my ( $ip, $name, $mac ) = split(/ /,$_);
18         $mac2ip->{$mac} = $ip;
19         $mac2name->{$mac} = $name;
20 }
21
22 warn "# mac2name = ",dump($mac2name);
23
24 foreach my $file ( glob('log/*lldp*') ) {
25         my ( undef, $name, undef ) = split(/_/, $file);
26         #print "# $name $file\n";
27
28         my $line_regex;
29         my @ports;
30
31         open(my $f, '<', $file);
32         while(<$f>) {
33                 chomp;
34                 #print "## $_<--\n";
35                 next if ( /^$/ || /^\s+Port/ );
36                 if ( /^--+/ ) {
37                         $line_regex = $_;
38                         $line_regex =~ s/\s+$//;
39                         $line_regex =~ s/-/./g;
40                         $line_regex =~ s/^/(/g;
41                         $line_regex =~ s/ /) (/g;
42                         $line_regex =~ s/$/)/g;
43                         #print "## line_regex = $line_regex\n";
44                         next;
45                 }
46                 if ( defined($line_regex) &&  /$line_regex/ ) {
47                         # Port       Device ID          Port ID          System Name     Capabilities 
48                         my @v = ( $1, $2, $3, $4, $5 );
49                         @v = map { s/^\s+//; s/\s+$//; $_ } @v;
50
51                         if ( length($v[1]) == 6 ) { # decode text mac
52                                 $v[1] = unpack('H*', $v[1]);
53                                 $v[1] =~ s/(..)/$1:/g;
54                                 $v[1] =~ s/:$//;
55                         }
56
57                         if ( exists $mac2name->{$v[1]} ) {
58                                 my $mac_name = $mac2name->{$v[1]};
59                                 if ( $v[3] eq '' ) {
60                                         $v[3] = $mac_name;
61                                 } else {
62                                         warn "ERROR: name different $v[3] != $mac_name" if $v[3] ne $mac_name;
63                                 }
64                         }
65
66                         #my ( $port, $device_id, $port_id, $system_name, $cap ) = @v;
67                         if ( $v[0] =~ m/^$/ ) {
68                                 my @old = @{ pop @ports };
69                                 foreach my $i ( 0 .. $#old ) {
70                                         $old[$i] .= $v[$i];
71                                 }
72                                 push @ports, [ @old ];
73                         } else {
74                                 push @ports, [ @v ];
75                         }
76                 } else {
77                         warn "# $_<--\n";
78                 }
79         }
80
81         foreach my $p ( @ports ) {
82                 print "$name ", join(' | ', @$p ), "\n";
83         }
84 }