32 |
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 File::Spec; # find location of script so we can put storage in same directory
|
|
|
10 |
use File::Basename;
|
|
|
11 |
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
|
|
|
12 |
use POSIX qw( strftime ); # to convert timestamp to something readable
|
|
|
13 |
|
|
|
14 |
our $VERSION = '1.1';
|
|
|
15 |
|
|
|
16 |
# where the script is located
|
|
|
17 |
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
|
|
|
18 |
# put the statefile in there
|
|
|
19 |
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
|
|
|
20 |
|
|
|
21 |
my %switchports;
|
|
|
22 |
|
|
|
23 |
# pretty up the MAC addresses by inserting colons between
|
|
|
24 |
# every pair of numbers
|
|
|
25 |
sub prettyMAC {
|
|
|
26 |
my $mac = shift;
|
|
|
27 |
my @mac = ( $mac =~ m/../g );
|
|
|
28 |
return join( ':', @mac );
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
|
|
|
32 |
sub prettyTime {
|
|
|
33 |
my $time = shift;
|
|
|
34 |
return '' unless $time;
|
|
|
35 |
return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
sub dumpLine {
|
|
|
39 |
my $delimiter = shift;
|
|
|
40 |
my $encapsulate = shift;
|
34 |
rodolico |
41 |
# read in values, defaulting to empty string if undef
|
|
|
42 |
my @values = map{ $_ ? $_ : '' } @_;
|
32 |
rodolico |
43 |
my $return;
|
|
|
44 |
@values = ( map{ $encapsulate . $_ . $encapsulate } @values ) if $encapsulate;
|
|
|
45 |
return join( $delimiter, @values ) . "\n";
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
# read the saved state into memory if it exists
|
|
|
50 |
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
|
|
|
51 |
|
|
|
52 |
# get the delimiter and the encapsulation chars
|
|
|
53 |
my $delimiter = shift;
|
|
|
54 |
my $encapsulate = shift;
|
|
|
55 |
# default to tab delimited, no encapsulation if none entered
|
|
|
56 |
$delimiter = "\t" unless $delimiter;
|
|
|
57 |
$encapsulate = '' unless $encapsulate;
|
|
|
58 |
|
|
|
59 |
my $yaml = YAML::Tiny->read( $STATEFILE );
|
|
|
60 |
%switchports = %{ $yaml->[0] };
|
|
|
61 |
|
|
|
62 |
foreach my $switch ( sort keys %switchports ) {
|
|
|
63 |
print "$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}\n";
|
|
|
64 |
# print the header
|
|
|
65 |
print &dumpLine( $delimiter,$encapsulate,'Switch','Port','Alias','MAC','IP','Host Name','Last Seen' );
|
|
|
66 |
foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
|
|
|
67 |
foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
|
|
|
68 |
next if $connection eq 'alias';
|
|
|
69 |
print &dumpLine ( $delimiter,$encapsulate,
|
|
|
70 |
$switch,
|
|
|
71 |
$port,
|
|
|
72 |
$switchports{$switch}{'ports'}{$port}{'alias'},
|
|
|
73 |
&prettyMAC( $switchports{$switch}{'ports'}{$port}{$connection}{'mac'}),
|
|
|
74 |
$switchports{$switch}{'ports'}{$port}{$connection}{'ip'},
|
|
|
75 |
$switchports{$switch}{'ports'}{$port}{$connection}{'hostname'},
|
|
|
76 |
&prettyTime( $switchports{$switch}{'ports'}{$port}{$connection}{'lastseen'} )
|
|
|
77 |
) ;
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
1;
|