Subversion Repositories sysadmin_scripts

Rev

Rev 66 | Rev 136 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 66 Rev 133
Line 3... Line 3...
3
# take persistent storage created by mapSwitches.pl and dump entries
3
# take persistent storage created by mapSwitches.pl and dump entries
4
# to a tab delimited text file on STDOUT.
4
# to a tab delimited text file on STDOUT.
5
 
5
 
6
# v1.2 RWR 20200228
6
# v1.2 RWR 20200228
7
# added description field
7
# added description field
-
 
8
# v3.0.0 RWR 20230325
-
 
9
# rewrite to use new data file format
8
 
10
 
9
use strict;
11
use strict;
10
use warnings;
12
use warnings;
11
 
13
 
12
use File::Spec; # find location of script so we can put storage in same directory
14
use File::Spec; # find location of script so we can put storage in same directory
13
use File::Basename;
15
use File::Basename;
14
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
16
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
15
use POSIX qw( strftime ); # to convert timestamp to something readable
17
use POSIX qw( strftime ); # to convert timestamp to something readable
-
 
18
use Data::Dumper; # only used for debugging
16
 
19
 
17
our $VERSION = '1.2';
20
our $VERSION = '3.0.0';
18
 
21
 
19
# where the script is located
22
# where the script is located
20
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
23
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
21
# put the statefile in there
24
# put the statefile in there
22
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
25
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
Line 46... Line 49...
46
   my $return;
49
   my $return;
47
   @values = ( map{ $encapsulate . $_ . $encapsulate } @values ) if $encapsulate;
50
   @values = ( map{ $encapsulate . $_ . $encapsulate } @values ) if $encapsulate;
48
   return join( $delimiter, @values ) . "\n";
51
   return join( $delimiter, @values ) . "\n";
49
}
52
}
50
 
53
 
-
 
54
# sorts by first column (switch name), second column (switch port number)
-
 
55
# then third column (mac address )
-
 
56
# NOTE: this is not csv safe; only works for tab delimited
-
 
57
sub sortArray {
-
 
58
   my @a = split( "\t", $a );
-
 
59
   my @b = split( "\t", $b );
-
 
60
   return $a[0] cmp $b[0] || $a[1] <=> $b[1] || $a[4] cmp $b[4];
-
 
61
}
-
 
62
 
51
 
63
 
52
# read the saved state into memory if it exists
64
# read the saved state into memory if it exists
53
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
65
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
54
 
66
 
55
# get the delimiter and the encapsulation chars
67
# get the delimiter and the encapsulation chars
Line 58... Line 70...
58
# default to tab delimited, no encapsulation if none entered
70
# default to tab delimited, no encapsulation if none entered
59
$delimiter = "\t" unless $delimiter;
71
$delimiter = "\t" unless $delimiter;
60
$encapsulate = '' unless $encapsulate;
72
$encapsulate = '' unless $encapsulate;
61
 
73
 
62
my $yaml = YAML::Tiny->read( $STATEFILE );
74
my $yaml = YAML::Tiny->read( $STATEFILE );
63
%switchports = %{ $yaml->[0] };
-
 
64
 
75
 
65
foreach my $switch ( sort keys %switchports ) {
76
my $saveFile = $yaml->[0];
66
   print "$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}\n";
-
 
67
   # print the header
77
#print Dumper( $saveFile ); die;
68
   print &dumpLine(  $delimiter,$encapsulate,'Switch','Port','Alias','Description','MAC','IP','Host Name','Last Seen' );
78
# print Dumper( $saveFile->{'macList'} );
-
 
79
 
-
 
80
my @out;
-
 
81
 
69
   foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
82
foreach my $mac ( keys %{ $saveFile->{'macList'} } ) {
70
      foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
83
   my $switchName = $saveFile->{'switchInfo'}->{$saveFile->{'macList'}->{$mac}->{'switch'}}->{'name'};
71
         next if $connection eq 'alias' or $connection eq 'description';
84
   my $portAlias =  $saveFile->{'switchInfo'}->{$saveFile->{'macList'}->{$mac}->{'switch'}}->{'ports'}->{$saveFile->{'macList'}->{$mac}->{'port'}}->{'alias'};
72
         print &dumpLine ( $delimiter,$encapsulate,
85
   push @out, &dumpLine( 
73
            $switch,
86
      $delimiter, 
74
            $port,
87
      $encapsulate, 
75
            $switchports{$switch}{'ports'}{$port}{'alias'},
88
      $saveFile->{'macList'}->{$mac}->{'switch'}, 
76
            $switchports{$switch}{'ports'}{$port}{'description'},
89
      $saveFile->{'macList'}->{$mac}->{'port'},
-
 
90
      $switchName,
-
 
91
      $portAlias,
77
            &prettyMAC( $switchports{$switch}{'ports'}{$port}{$connection}{'mac'}),
92
      &prettyMAC( $mac ),
78
            $switchports{$switch}{'ports'}{$port}{$connection}{'ip'},
93
      $saveFile->{'macList'}->{$mac}->{'ip'},
79
            $switchports{$switch}{'ports'}{$port}{$connection}{'hostname'},
94
      $saveFile->{'macList'}->{$mac}->{'hostname'},
80
            &prettyTime( $switchports{$switch}{'ports'}{$port}{$connection}{'lastseen'} )
95
      &prettyTime( $saveFile->{'macList'}->{$mac}->{'lastseen'} )
81
         ) ;
-
 
82
      }
-
 
83
   }
96
   );
84
}
97
}
85
 
98
 
-
 
99
print &dumpLine(
-
 
100
   $delimiter, 
-
 
101
   $encapsulate, 
-
 
102
   'Switch','Port','Alias','Description','MAC','IP','Host Name','Last Seen'
-
 
103
   );
-
 
104
 
-
 
105
print sort sortArray @out;
-
 
106
 
86
1;
107
1;