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