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 |
|
136 |
rodolico |
27 |
# see https://perldoc.perl.org/Getopt/Long.html
|
|
|
28 |
use Getopt::Long;
|
|
|
29 |
# allow -vvn (ie, --verbose --verbose --dryrun)
|
|
|
30 |
Getopt::Long::Configure ("bundling");
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
32 |
rodolico |
34 |
my %switchports;
|
|
|
35 |
|
|
|
36 |
# pretty up the MAC addresses by inserting colons between
|
|
|
37 |
# every pair of numbers
|
|
|
38 |
sub prettyMAC {
|
|
|
39 |
my $mac = shift;
|
|
|
40 |
my @mac = ( $mac =~ m/../g );
|
|
|
41 |
return join( ':', @mac );
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
# change the Unix timestamp into yyyy-mm-dd hh:mm:ss
|
|
|
45 |
sub prettyTime {
|
|
|
46 |
my $time = shift;
|
|
|
47 |
return '' unless $time;
|
|
|
48 |
return strftime '%Y-%m-%d %H:%M:%S', localtime( $time );
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
sub dumpLine {
|
|
|
52 |
my $delimiter = shift;
|
|
|
53 |
my $encapsulate = shift;
|
34 |
rodolico |
54 |
# read in values, defaulting to empty string if undef
|
|
|
55 |
my @values = map{ $_ ? $_ : '' } @_;
|
32 |
rodolico |
56 |
my $return;
|
|
|
57 |
@values = ( map{ $encapsulate . $_ . $encapsulate } @values ) if $encapsulate;
|
|
|
58 |
return join( $delimiter, @values ) . "\n";
|
|
|
59 |
}
|
|
|
60 |
|
133 |
rodolico |
61 |
# sorts by first column (switch name), second column (switch port number)
|
|
|
62 |
# then third column (mac address )
|
|
|
63 |
# NOTE: this is not csv safe; only works for tab delimited
|
|
|
64 |
sub sortArray {
|
|
|
65 |
my @a = split( "\t", $a );
|
|
|
66 |
my @b = split( "\t", $b );
|
|
|
67 |
return $a[0] cmp $b[0] || $a[1] <=> $b[1] || $a[4] cmp $b[4];
|
|
|
68 |
}
|
32 |
rodolico |
69 |
|
133 |
rodolico |
70 |
|
136 |
rodolico |
71 |
# simple display if --help is passed
|
|
|
72 |
sub help {
|
|
|
73 |
use File::Basename;
|
|
|
74 |
print basename($0) . " $VERSION\n";
|
|
|
75 |
print <<END
|
|
|
76 |
$0 [options]
|
|
|
77 |
Reads mapSwitches.yaml and emits the result as a tab delimited text file, with headers.
|
|
|
78 |
Output is sorted by switch name, switch port number, then mac address.
|
|
|
79 |
Options:
|
|
|
80 |
--version - display version and exit
|
|
|
81 |
--help - This page
|
|
|
82 |
END
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
# handle any command line parameters that may have been passed in
|
|
|
86 |
my $version = 0; # just used to determine if we should display the version
|
|
|
87 |
my $help = 0; # also if we want help
|
|
|
88 |
GetOptions (
|
|
|
89 |
'help|h' => \$help,
|
|
|
90 |
'version|v' => \$version,
|
|
|
91 |
) or die "Error parsing command line\n";
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
if ( $help ) { &help() ; exit; }
|
|
|
95 |
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
|
|
|
96 |
|
|
|
97 |
|
32 |
rodolico |
98 |
# read the saved state into memory if it exists
|
|
|
99 |
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
|
|
|
100 |
|
|
|
101 |
# get the delimiter and the encapsulation chars
|
|
|
102 |
my $delimiter = shift;
|
|
|
103 |
my $encapsulate = shift;
|
|
|
104 |
# default to tab delimited, no encapsulation if none entered
|
|
|
105 |
$delimiter = "\t" unless $delimiter;
|
|
|
106 |
$encapsulate = '' unless $encapsulate;
|
|
|
107 |
|
|
|
108 |
my $yaml = YAML::Tiny->read( $STATEFILE );
|
|
|
109 |
|
133 |
rodolico |
110 |
my $saveFile = $yaml->[0];
|
|
|
111 |
#print Dumper( $saveFile ); die;
|
|
|
112 |
# print Dumper( $saveFile->{'macList'} );
|
|
|
113 |
|
|
|
114 |
my @out;
|
|
|
115 |
|
|
|
116 |
foreach my $mac ( keys %{ $saveFile->{'macList'} } ) {
|
|
|
117 |
my $switchName = $saveFile->{'switchInfo'}->{$saveFile->{'macList'}->{$mac}->{'switch'}}->{'name'};
|
|
|
118 |
my $portAlias = $saveFile->{'switchInfo'}->{$saveFile->{'macList'}->{$mac}->{'switch'}}->{'ports'}->{$saveFile->{'macList'}->{$mac}->{'port'}}->{'alias'};
|
|
|
119 |
push @out, &dumpLine(
|
|
|
120 |
$delimiter,
|
|
|
121 |
$encapsulate,
|
|
|
122 |
$saveFile->{'macList'}->{$mac}->{'switch'},
|
|
|
123 |
$saveFile->{'macList'}->{$mac}->{'port'},
|
|
|
124 |
$switchName,
|
|
|
125 |
$portAlias,
|
|
|
126 |
&prettyMAC( $mac ),
|
|
|
127 |
$saveFile->{'macList'}->{$mac}->{'ip'},
|
|
|
128 |
$saveFile->{'macList'}->{$mac}->{'hostname'},
|
|
|
129 |
&prettyTime( $saveFile->{'macList'}->{$mac}->{'lastseen'} )
|
|
|
130 |
);
|
32 |
rodolico |
131 |
}
|
|
|
132 |
|
133 |
rodolico |
133 |
print &dumpLine(
|
|
|
134 |
$delimiter,
|
|
|
135 |
$encapsulate,
|
136 |
rodolico |
136 |
'switch','port','alias','description','mac','ip','host_name','last_seen'
|
133 |
rodolico |
137 |
);
|
|
|
138 |
|
|
|
139 |
print sort sortArray @out;
|
|
|
140 |
|
32 |
rodolico |
141 |
1;
|