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
133 rodolico 8
# v3.0.0 RWR 20230325
9
# rewrite to use new data file format
66 rodolico 10
 
32 rodolico 11
use strict;
12
use warnings;
13
 
14
use File::Spec; # find location of script so we can put storage in same directory
15
use File::Basename;
16
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
17
use POSIX qw( strftime ); # to convert timestamp to something readable
133 rodolico 18
use Data::Dumper; # only used for debugging
32 rodolico 19
 
133 rodolico 20
our $VERSION = '3.0.0';
32 rodolico 21
 
22
# where the script is located
23
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
24
# put the statefile in there
25
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
26
 
27
my %switchports;
28
 
29
# pretty up the MAC addresses by inserting colons between
30
# every pair of numbers
31
sub prettyMAC {
32
   my $mac = shift;
33
   my @mac = ( $mac =~ m/../g );
34
   return join( ':', @mac );
35
}
36
 
37
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
38
sub prettyTime {
39
   my $time = shift;
40
   return '' unless $time;
41
   return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
42
}
43
 
44
sub dumpLine {
45
   my $delimiter = shift;
46
   my $encapsulate = shift;
34 rodolico 47
   # read in values, defaulting to empty string if undef
48
   my @values = map{ $_ ? $_ : '' } @_;
32 rodolico 49
   my $return;
50
   @values = ( map{ $encapsulate . $_ . $encapsulate } @values ) if $encapsulate;
51
   return join( $delimiter, @values ) . "\n";
52
}
53
 
133 rodolico 54
# sorts by first column (switch name), second column (switch port number)
55
# then third column (mac address )
56
# NOTE: this is not csv safe; only works for tab delimited
57
sub sortArray {
58
   my @a = split( "\t", $a );
59
   my @b = split( "\t", $b );
60
   return $a[0] cmp $b[0] || $a[1] <=> $b[1] || $a[4] cmp $b[4];
61
}
32 rodolico 62
 
133 rodolico 63
 
32 rodolico 64
# read the saved state into memory if it exists
65
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
66
 
67
# get the delimiter and the encapsulation chars
68
my $delimiter = shift;
69
my $encapsulate = shift;
70
# default to tab delimited, no encapsulation if none entered
71
$delimiter = "\t" unless $delimiter;
72
$encapsulate = '' unless $encapsulate;
73
 
74
my $yaml = YAML::Tiny->read( $STATEFILE );
75
 
133 rodolico 76
my $saveFile = $yaml->[0];
77
#print Dumper( $saveFile ); die;
78
# print Dumper( $saveFile->{'macList'} );
79
 
80
my @out;
81
 
82
foreach my $mac ( keys %{ $saveFile->{'macList'} } ) {
83
   my $switchName = $saveFile->{'switchInfo'}->{$saveFile->{'macList'}->{$mac}->{'switch'}}->{'name'};
84
   my $portAlias =  $saveFile->{'switchInfo'}->{$saveFile->{'macList'}->{$mac}->{'switch'}}->{'ports'}->{$saveFile->{'macList'}->{$mac}->{'port'}}->{'alias'};
85
   push @out, &dumpLine( 
86
      $delimiter, 
87
      $encapsulate, 
88
      $saveFile->{'macList'}->{$mac}->{'switch'}, 
89
      $saveFile->{'macList'}->{$mac}->{'port'},
90
      $switchName,
91
      $portAlias,
92
      &prettyMAC( $mac ),
93
      $saveFile->{'macList'}->{$mac}->{'ip'},
94
      $saveFile->{'macList'}->{$mac}->{'hostname'},
95
      &prettyTime( $saveFile->{'macList'}->{$mac}->{'lastseen'} )
96
   );
32 rodolico 97
}
98
 
133 rodolico 99
print &dumpLine(
100
   $delimiter, 
101
   $encapsulate, 
102
   'Switch','Port','Alias','Description','MAC','IP','Host Name','Last Seen'
103
   );
104
 
105
print sort sortArray @out;
106
 
32 rodolico 107
1;