Subversion Repositories sysadmin_scripts

Rev

Rev 133 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 133 Rev 136
Line 22... Line 22...
22
# where the script is located
22
# where the script is located
23
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
23
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
24
# put the statefile in there
24
# put the statefile in there
25
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
25
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
26
 
26
 
-
 
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
 
27
my %switchports;
34
my %switchports;
28
 
35
 
29
# pretty up the MAC addresses by inserting colons between
36
# pretty up the MAC addresses by inserting colons between
30
# every pair of numbers
37
# every pair of numbers
31
sub prettyMAC {
38
sub prettyMAC {
Line 59... Line 66...
59
   my @b = split( "\t", $b );
66
   my @b = split( "\t", $b );
60
   return $a[0] cmp $b[0] || $a[1] <=> $b[1] || $a[4] cmp $b[4];
67
   return $a[0] cmp $b[0] || $a[1] <=> $b[1] || $a[4] cmp $b[4];
61
}
68
}
62
 
69
 
63
 
70
 
-
 
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
 
64
# read the saved state into memory if it exists
98
# read the saved state into memory if it exists
65
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
99
die "no state file found, looking at $STATEFILE\n" unless -e $STATEFILE;
66
 
100
 
67
# get the delimiter and the encapsulation chars
101
# get the delimiter and the encapsulation chars
68
my $delimiter = shift;
102
my $delimiter = shift;
Line 97... Line 131...
97
}
131
}
98
 
132
 
99
print &dumpLine(
133
print &dumpLine(
100
   $delimiter, 
134
   $delimiter, 
101
   $encapsulate, 
135
   $encapsulate, 
102
   'Switch','Port','Alias','Description','MAC','IP','Host Name','Last Seen'
136
   'switch','port','alias','description','mac','ip','host_name','last_seen'
103
   );
137
   );
104
 
138
 
105
print sort sortArray @out;
139
print sort sortArray @out;
106
 
140
 
107
1;
141
1;