Subversion Repositories sysadmin_scripts

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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';
23
 
24
my %switchports;
25
 
26
# pretty up the MAC addresses by inserting colons between
27
# every pair of numbers
28
sub prettyMAC {
29
   my $mac = shift;
30
   my @mac = ( $mac =~ m/../g );
31
   return join( ':', @mac );
32
}
33
 
34
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
35
sub prettyTime {
36
   my $time = shift;
37
   return '' unless $time;
38
   return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
39
}
40
 
41
sub dumpLine {
128 rodolico 42
   my $startTag = shift;
43
   my $endTag = shift;
127 rodolico 44
   # read in values, defaulting to empty string if undef
45
   my @values = map{ $_ ? $_ : '' } @_;
128 rodolico 46
   return "<tr>$startTag" . join( $endTag . $startTag, @values ) . "$endTag</tr>\n";
127 rodolico 47
}
48
 
49
 
50
# read the saved state into memory if it exists
51
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
52
 
53
# get the delimiter and the encapsulation chars
54
my $delimiter = shift;
55
my $encapsulate = shift;
56
# default to tab delimited, no encapsulation if none entered
57
$delimiter = "\t" unless $delimiter;
58
$encapsulate = '' unless $encapsulate;
59
 
60
my $yaml = YAML::Tiny->read( $STATEFILE );
61
%switchports = %{ $yaml->[0] };
62
 
63
print "<html>\n<body>\n";
128 rodolico 64
print "<h1>MapSwitches</h1><h2>Generated: " . &prettyTime(time) . "</h2>\n";
127 rodolico 65
print "<table border='1'>\n";
66
 
67
foreach my $switch ( sort keys %switchports ) {
128 rodolico 68
   print "<tr><td colspan='8' align='center'>$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}</td></tr>\n";
127 rodolico 69
   # print the header
128 rodolico 70
   print &dumpLine( '<th>', '</th>',  'Switch','Port','Alias','Description','MAC','IP','Host Name','Last Seen' );
127 rodolico 71
   foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
72
      foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
73
         next if $connection eq 'alias' or $connection eq 'description';
128 rodolico 74
         print &dumpLine ( '<td>', '</td>',
127 rodolico 75
            $switch,
76
            $port,
77
            $switchports{$switch}{'ports'}{$port}{'alias'},
78
            $switchports{$switch}{'ports'}{$port}{'description'},
79
            &prettyMAC( $switchports{$switch}{'ports'}{$port}{$connection}{'mac'}),
80
            $switchports{$switch}{'ports'}{$port}{$connection}{'ip'},
81
            $switchports{$switch}{'ports'}{$port}{$connection}{'hostname'},
82
            &prettyTime( $switchports{$switch}{'ports'}{$port}{$connection}{'lastseen'} )
83
         ) ;
84
      }
85
   }
86
   print "</table>\n</body>\n</html>\n";
87
}
88
 
89
1;