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
 
9
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
10
use POSIX qw( strftime ); # to convert timestamp to something readable
11
 
12
my $STATEFILE = 'mapSwitches.yaml';
13
 
14
my %switchports;
15
 
16
# pretty up the MAC addresses by inserting colons between
17
# every pair of numbers
18
sub prettyMAC {
19
   my $mac = shift;
20
   my @mac = ( $mac =~ m/../g );
21
   return join( ':', @mac );
22
}
23
 
24
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
25
sub prettyTime {
26
   my $time = shift;
27
   return '' unless $time;
28
   return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
29
}
30
 
31
 
32
# read the saved state into memory if it exists
33
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE
34
 
35
my $yaml = YAML::Tiny->read( $STATEFILE );
36
%switchports = %{ $yaml->[0] };
37
 
38
foreach my $switch ( sort keys %switchports ) {
39
   print "$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}\n";
40
   print "Switch\tPort\tMAC\tIP\tHost Name\tLast Seen\n";
41
   foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
42
      foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
43
         print "$switch\t" .
44
               "$port\t" .
45
               &prettyMAC( $switchports{$switch}{ports}{$port}{$connection}{'mac'}) . "\t" .
46
               "$switchports{$switch}{ports}{$port}{$connection}{'ip'}\t" .
47
               "$switchports{$switch}{ports}{$port}{$connection}{'hostname'}\t" .
48
               &prettyTime( $switchports{$switch}{ports}{$port}{$connection}{'lastseen'} ) . "\n";
49
      }
50
   }
51
}
52
 
53
1;