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