Subversion Repositories sysadmin_scripts

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 rodolico 1
#! /usr/bin/env perl
2
 
3
# take persistent storage created by mapSwitches.pl and dump entries
4
# to a tab delimited text file on STDOUT.
5
 
6
use strict;
7
use warnings;
8
 
6 rodolico 9
use File::Spec; # find location of script so we can put storage in same directory
10
use File::Basename;
2 rodolico 11
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
12
use POSIX qw( strftime ); # to convert timestamp to something readable
13
 
6 rodolico 14
# where the script is located
15
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
16
# put the statefile in there
17
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
2 rodolico 18
 
19
my %switchports;
20
 
21
# pretty up the MAC addresses by inserting colons between
22
# every pair of numbers
23
sub prettyMAC {
24
   my $mac = shift;
25
   my @mac = ( $mac =~ m/../g );
26
   return join( ':', @mac );
27
}
28
 
29
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
30
sub prettyTime {
31
   my $time = shift;
32
   return '' unless $time;
33
   return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
34
}
35
 
36
 
37
# read the saved state into memory if it exists
4 rodolico 38
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
2 rodolico 39
 
40
my $yaml = YAML::Tiny->read( $STATEFILE );
41
%switchports = %{ $yaml->[0] };
42
 
43
foreach my $switch ( sort keys %switchports ) {
44
   print "$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}\n";
45
   print "Switch\tPort\tMAC\tIP\tHost Name\tLast Seen\n";
46
   foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
47
      foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
48
         print "$switch\t" .
49
               "$port\t" .
50
               &prettyMAC( $switchports{$switch}{ports}{$port}{$connection}{'mac'}) . "\t" .
51
               "$switchports{$switch}{ports}{$port}{$connection}{'ip'}\t" .
52
               "$switchports{$switch}{ports}{$port}{$connection}{'hostname'}\t" .
53
               &prettyTime( $switchports{$switch}{ports}{$port}{$connection}{'lastseen'} ) . "\n";
54
      }
55
   }
56
}
57
 
58
1;