Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
66 rodolico 6
# v1.2 RWR 20200228
7
# added description field
8
 
32 rodolico 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
 
66 rodolico 17
our $VERSION = '1.2';
32 rodolico 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 {
42
   my $delimiter = shift;
43
   my $encapsulate = shift;
34 rodolico 44
   # read in values, defaulting to empty string if undef
45
   my @values = map{ $_ ? $_ : '' } @_;
32 rodolico 46
   my $return;
47
   @values = ( map{ $encapsulate . $_ . $encapsulate } @values ) if $encapsulate;
48
   return join( $delimiter, @values ) . "\n";
49
}
50
 
51
 
52
# read the saved state into memory if it exists
53
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
54
 
55
# get the delimiter and the encapsulation chars
56
my $delimiter = shift;
57
my $encapsulate = shift;
58
# default to tab delimited, no encapsulation if none entered
59
$delimiter = "\t" unless $delimiter;
60
$encapsulate = '' unless $encapsulate;
61
 
62
my $yaml = YAML::Tiny->read( $STATEFILE );
63
%switchports = %{ $yaml->[0] };
64
 
65
foreach my $switch ( sort keys %switchports ) {
66
   print "$switchports{$switch}{'name'} Located at $switchports{$switch}{'location'}\n";
67
   # print the header
66 rodolico 68
   print &dumpLine(  $delimiter,$encapsulate,'Switch','Port','Alias','Description','MAC','IP','Host Name','Last Seen' );
32 rodolico 69
   foreach my $port ( sort {$a<=>$b} keys %{$switchports{$switch}{'ports'}} ) {
70
      foreach my $connection ( keys %{$switchports{$switch}{'ports'}{$port} } ) {
66 rodolico 71
         next if $connection eq 'alias' or $connection eq 'description';
32 rodolico 72
         print &dumpLine ( $delimiter,$encapsulate,
73
            $switch,
74
            $port,
75
            $switchports{$switch}{'ports'}{$port}{'alias'},
66 rodolico 76
            $switchports{$switch}{'ports'}{$port}{'description'},
32 rodolico 77
            &prettyMAC( $switchports{$switch}{'ports'}{$port}{$connection}{'mac'}),
78
            $switchports{$switch}{'ports'}{$port}{$connection}{'ip'},
79
            $switchports{$switch}{'ports'}{$port}{$connection}{'hostname'},
80
            &prettyTime( $switchports{$switch}{'ports'}{$port}{$connection}{'lastseen'} )
81
         ) ;
82
      }
83
   }
84
}
85
 
86
1;