Rev 2 | Blame | Last modification | View Log | Download | RSS feed
#! /usr/bin/env perl
# take persistent storage created by mapSwitches.pl and dump entries
# to a tab delimited text file on STDOUT.
use strict;
use warnings;
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
use POSIX qw( strftime ); # to convert timestamp to something readable
my $STATEFILE = 'mapSwitches.yaml';
my %switchports;
# pretty up the MAC addresses by inserting colons between
# every pair of numbers
sub prettyMAC {
my $mac = shift;
my @mac = ( $mac =~ m/../g );
return join( ':', @mac );
}
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
sub prettyTime {
my $time = shift;
return '' unless $time;
return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
}
# read the saved state into memory if it exists
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
my $yaml = YAML::Tiny->read( $STATEFILE );
%switchports = %{ $yaml->[0] };
foreach my $switch ( sort keys %switchports ) {
print "$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}\n";
print "Switch\tPort\tMAC\tIP\tHost Name\tLast Seen\n";
foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
print "$switch\t" .
"$port\t" .
&prettyMAC( $switchports{$switch}{ports}{$port}{$connection}{'mac'}) . "\t" .
"$switchports{$switch}{ports}{$port}{$connection}{'ip'}\t" .
"$switchports{$switch}{ports}{$port}{$connection}{'hostname'}\t" .
&prettyTime( $switchports{$switch}{ports}{$port}{$connection}{'lastseen'} ) . "\n";
}
}
}
1;