133 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
# Script does an snmp walk through the ports of one or more network
|
|
|
4 |
# switch, gathering MAC addresses assigned to each port.
|
|
|
5 |
#
|
|
|
6 |
# It then does an snmp walk through the arp table of one or
|
|
|
7 |
# more routers to determine IP and DNS entries for the MAC address
|
|
|
8 |
#
|
|
|
9 |
# Information gathered is stored in persistent storage (a yaml formatted file
|
|
|
10 |
# in the same directory), then reloaded at the next start of the program.
|
|
|
11 |
# As new entries become available, they are added. A time stamp
|
|
|
12 |
# records the last time an entry was "seen"
|
|
|
13 |
#
|
|
|
14 |
# requires snmp running on this machine. Also uses YAML::Tiny perl module
|
|
|
15 |
# under debian and derivatives, use the following command to install
|
|
|
16 |
# apt-get -y install snmp libyaml-tiny-perl
|
|
|
17 |
#
|
|
|
18 |
#
|
|
|
19 |
# Data is stored in the hash %switchports with the following structure
|
|
|
20 |
# %switchports =
|
|
|
21 |
# {switch} (from $config{'switches'} key)
|
|
|
22 |
# {name} (scalar, from snmp)
|
|
|
23 |
# {location} (scalar, from snmp)
|
|
|
24 |
# {'ports'} (constant key for sub hash)
|
|
|
25 |
# {port number} (from snmp)
|
|
|
26 |
# {connection} (uniqe id from snmp, basically the MAC)
|
|
|
27 |
# {mac} (from snmp walk of switch)
|
|
|
28 |
# {ip} (from snmp walk of router)
|
|
|
29 |
# {hostname} (from reverse dns query)
|
|
|
30 |
# {lastseen} (last time this was active as unix timestamp)
|
|
|
31 |
#
|
|
|
32 |
# Uses two external files, both in the same directory as the script
|
|
|
33 |
# mapSwitches.config.yaml - Configuration file. See makeConfig.pl.sample for documentation. This can be created if you, like
|
|
|
34 |
# me, are more comfortable with perl syntax. Once edited, run it to create the .yaml config file
|
|
|
35 |
# mapSwitches.yaml - a state file which maintains information across executions. This allows us to track connections
|
|
|
36 |
# which are not on all the time. Can be deleted at any time for a fresh start. Can be dumped into a
|
|
|
37 |
# tab delimited text file (to STDOUT) with helper script mapSwitchesCSV.pl
|
|
|
38 |
#
|
|
|
39 |
|
|
|
40 |
# 20190407 RWR
|
|
|
41 |
# converted to use external config file in YAML format
|
|
|
42 |
# added the ability to ignore ports on the switches
|
|
|
43 |
# 20190514 RWR
|
|
|
44 |
# Added port aliases
|
|
|
45 |
# 20190526 RWR
|
|
|
46 |
# fixed mapSwitchCSV.pl to output in various delimited format (see README)
|
|
|
47 |
# 20200107 RWR v1.3
|
|
|
48 |
# Fixed problem where alias MIB was not returning values
|
|
|
49 |
# 20200228 RWR v1.4
|
|
|
50 |
# added description MIB and field
|
|
|
51 |
# 20200229 RWR v2.0.0
|
|
|
52 |
# rewrite, with 80% of the code changed. Found serious flaws with the way the ports were being read and fixed them.
|
|
|
53 |
# also set it up so it is a little more concise by combining redundant code into one routine, parseSNMPQuery, which
|
|
|
54 |
# reads an snmp walk and returns the results as a hash
|
|
|
55 |
# 20200301 RWR v2.1.0
|
|
|
56 |
# added some error checking in snmpwalk when a value is not returned, and also when the MAC address of the switch in
|
|
|
57 |
# question show up as bridge 0, which has no port (or, actually, all ports). Also, fixed the ignoreports
|
|
|
58 |
# 20200302 RWR v2.2.0
|
|
|
59 |
# Added the ability to override or supplement arp and dns searches for mac addresses
|
|
|
60 |
# 20230323 RWR v2.3.0
|
|
|
61 |
# Added ability to have one or more non_router files, with mac/name/ip in it in case arp lookup failed
|
|
|
62 |
# added script to create static html file in addition to csv
|
|
|
63 |
#
|
136 |
rodolico |
64 |
# 20230324 RWR v3.0.0
|
133 |
rodolico |
65 |
# rewrite to make things more efficient, and store data in different format. You MUST remove old yaml file
|
|
|
66 |
# before running this.
|
136 |
rodolico |
67 |
#
|
|
|
68 |
# 20230325 RWR v3.0.1
|
|
|
69 |
# Added ability to delete entries which haven't been seen since $config->{'ttl'} seconds. ttl can be
|
|
|
70 |
# an integer followed by a unit, with units being single char h,d,w,m,y (hours, days, weeks, months, years)
|
|
|
71 |
# added ability to set so it will only refresh dns and device names every $refresh seconds, decreasing
|
|
|
72 |
# run time to 33% value when not used. Test system decreases from 15s to 5-7s
|
|
|
73 |
# reorganized so system runs smoother (test system decreased from 40 seconds to 15)
|
138 |
rodolico |
74 |
#
|
|
|
75 |
# 20230326 RWR v3.0.2
|
|
|
76 |
# set it up so if net::dns not loadable, will revert to ionet
|
157 |
rodolico |
77 |
# 20240810 RWR v3.4.0
|
|
|
78 |
# Added code in getDNSNames to use dig if it is available and all other ways have failed to find a hostname
|
164 |
rodolico |
79 |
# 20240818 RWR v3.4.1
|
|
|
80 |
# Fixed so empty IP/Mac does not cause error
|
133 |
rodolico |
81 |
|
157 |
rodolico |
82 |
|
136 |
rodolico |
83 |
# for Debian, to get the required libraries
|
|
|
84 |
# apt install libyaml-tiny-perl libnet-dns-perl snmp
|
|
|
85 |
|
133 |
rodolico |
86 |
use strict;
|
|
|
87 |
use warnings;
|
|
|
88 |
use Data::Dumper; # only used for debugging
|
|
|
89 |
use Socket; # for reverse dns entries
|
|
|
90 |
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
|
|
|
91 |
use File::Spec; # find location of script so we can put storage in same directory
|
|
|
92 |
use File::Basename;
|
|
|
93 |
|
|
|
94 |
my $DEBUG = 0;
|
|
|
95 |
|
|
|
96 |
# define the version number
|
|
|
97 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
|
|
98 |
use version;
|
164 |
rodolico |
99 |
our $VERSION = version->declare("v3.4.1");
|
133 |
rodolico |
100 |
|
|
|
101 |
# see https://perldoc.perl.org/Getopt/Long.html
|
|
|
102 |
use Getopt::Long;
|
|
|
103 |
# allow -vvn (ie, --verbose --verbose --dryrun)
|
|
|
104 |
Getopt::Long::Configure ("bundling");
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
my %config; # we read the configuration file into this
|
|
|
109 |
|
|
|
110 |
# where the script is located
|
|
|
111 |
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
|
|
|
112 |
# put the statefile in there
|
|
|
113 |
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
|
|
|
114 |
my $CONFIGFILE = $scriptDir . '/mapSwitches.config.yaml';
|
|
|
115 |
|
136 |
rodolico |
116 |
|
|
|
117 |
|
133 |
rodolico |
118 |
my $SWITCHPORTMIB = 'iso.3.6.1.2.1.2.2.1.8'; # returns port number and state, with a state of '1' meaning operational
|
|
|
119 |
my $SWITCHMACMIB = 'iso.3.6.1.2.1.17.4.3.1.1'; # dot1dTpFdbAddress, aka mac addresses
|
|
|
120 |
my $SWITCHIFALIASMIB = 'iso.3.6.1.2.1.31.1.1.1.18'; # ifAlias, the the 'alias' field
|
|
|
121 |
my $SWITCHIFDESCMIB = 'iso.3.6.1.2.1.2.2.1.2'; # ifDescr, the "description" field
|
|
|
122 |
my $BRIDGEPORTSMIB = 'iso.3.6.1.2.1.17.4.3.1.2';
|
|
|
123 |
my $BRIDGE2PORTMIB = 'iso.3.6.1.2.1.17.1.4.1.2';
|
|
|
124 |
# this is only for the router, to get IP of the device in question
|
|
|
125 |
my $ROUTERIPMACMIB = 'iso.3.6.1.2.1.4.22.1.2'; # ipNetToMediaPhysAddress #'iso.3.6.1.2.1.3.1.1.2';
|
|
|
126 |
# this should work for all devices
|
|
|
127 |
my $DEVICENAMEMIB = 'iso.3.6.1.2.1.1.5'; # sysname
|
|
|
128 |
my $DEVICELOCMIB = 'iso.3.6.1.2.1.1.6'; # syslocation
|
|
|
129 |
|
|
|
130 |
# snmp returns a dotted integer for the mac, but we want mac's as hex with no formatting
|
|
|
131 |
# take a dotted integer form of a mac and converts it to hex, all lower case
|
|
|
132 |
sub makeMAC {
|
|
|
133 |
my $string = shift;
|
|
|
134 |
my @octets = split( '\.', $string );
|
|
|
135 |
for ( my $i = 0; $i < @octets; $i++ ) {
|
|
|
136 |
$octets[$i] = sprintf( "%02x", $octets[$i] );
|
|
|
137 |
}
|
|
|
138 |
return join( '', @octets );
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
# mac's from various places can have formatting, like ##:##:##:##:## or ###-###-###
|
|
|
142 |
# this will clean all that up by removing anything except hex values
|
|
|
143 |
# it will also convert alpha's (a-f) to lower case.
|
|
|
144 |
sub cleanMac {
|
|
|
145 |
my $mac = shift;
|
|
|
146 |
$mac =~ s/[^a-f0-9]//gi;
|
|
|
147 |
return lc $mac;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
# grabs one single SNMP value from string. Assumes $OID returns only one line, then runs $regex against it.
|
|
|
151 |
sub getOneSNMPValue {
|
|
|
152 |
my ( $oid, $community, $ip, $regex ) = @_;
|
|
|
153 |
my $line = `snmpwalk -v1 -c $community $ip $oid`;
|
|
|
154 |
$line =~ m/$regex/i;
|
|
|
155 |
return $1;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
# runs an snmpwalk using $MIB, then for each line, splits it with a regex
|
|
|
159 |
# if order is set to 12, first value of regex is a key, else second value is the key
|
|
|
160 |
sub parseSNMPQuery {
|
|
|
161 |
my ( $MIB, $ip, $community, $regex, $order ) = @_;
|
|
|
162 |
$order = 12 unless $order;
|
|
|
163 |
my $return = {};
|
|
|
164 |
my $values = `snmpwalk -v1 -c $community $ip $MIB`;
|
|
|
165 |
print "\t\t\tsnmpwalk -v1 -c $community $ip $MIB\n" if $DEBUG > 1;
|
|
|
166 |
foreach my $line ( split( "\n", $values ) ) {
|
|
|
167 |
next unless $line =~ m/$regex/;
|
|
|
168 |
if ( $order == 12 ) {
|
|
|
169 |
$return->{$1} = $2;
|
|
|
170 |
} else {
|
|
|
171 |
$return->{$2} = $1;
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
return $return;
|
|
|
175 |
}
|
|
|
176 |
|
136 |
rodolico |
177 |
# do a reverse dns lookup of an IP address and return the hostname
|
|
|
178 |
# note that $dns is an instance of Net::DNS
|
138 |
rodolico |
179 |
# if it is not set, will revert to using inet_aton, which only
|
|
|
180 |
# works off of the resolv.conf on the local machine
|
136 |
rodolico |
181 |
sub getHostName {
|
|
|
182 |
my ($dns, $ip ) = @_;
|
139 |
rodolico |
183 |
return '' unless $ip;
|
138 |
rodolico |
184 |
if ( $dns ) {
|
|
|
185 |
foreach my $resource ( @$dns ) {
|
|
|
186 |
my $reply = $resource->search( $ip );
|
|
|
187 |
next unless defined $reply;
|
|
|
188 |
my @answer = grep { ! /^((;.*)|(\s*))$/ } split( "\n", $reply->string );
|
|
|
189 |
$answer[0] =~ m/\s([a-z0-9.-]+)\.$/;
|
|
|
190 |
return $1;
|
|
|
191 |
}
|
|
|
192 |
} else {
|
|
|
193 |
return gethostbyaddr( inet_aton( $ip ), AF_INET );
|
136 |
rodolico |
194 |
}
|
|
|
195 |
return '';
|
|
|
196 |
}
|
133 |
rodolico |
197 |
|
|
|
198 |
# Load configuration file, die if we could not find it
|
|
|
199 |
# config file is a single page YAML file
|
|
|
200 |
sub loadConfig {
|
|
|
201 |
my $filename = shift;
|
|
|
202 |
my %config;
|
|
|
203 |
if ( -e $filename ) {
|
|
|
204 |
my $yaml = YAML::Tiny->read( $filename );
|
|
|
205 |
%config = %{ $yaml->[0] };
|
|
|
206 |
} else {
|
|
|
207 |
die "could not locate config file $filename\n";
|
|
|
208 |
}
|
|
|
209 |
return \%config;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
# simple display if --help is passed
|
|
|
213 |
sub help {
|
|
|
214 |
use File::Basename;
|
|
|
215 |
print basename($0) . " $VERSION\n";
|
|
|
216 |
print <<END
|
|
|
217 |
$0 [options]
|
|
|
218 |
Options:
|
136 |
rodolico |
219 |
--no-snmp - do not actually run snmp commands
|
|
|
220 |
--debug - set debug level
|
|
|
221 |
--refresh - force a refresh even if not time
|
|
|
222 |
--version - display version and exit
|
|
|
223 |
--help - This page
|
133 |
rodolico |
224 |
END
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
# get information about one single switch
|
|
|
228 |
#
|
|
|
229 |
sub getSwitchInfo {
|
136 |
rodolico |
230 |
my ( $ip, $community, $ignorePorts ) = @_;
|
|
|
231 |
my $switchInfo = {};
|
133 |
rodolico |
232 |
print "Working on $ip with community $community\n" if $DEBUG;
|
|
|
233 |
$switchInfo->{$ip}->{'name'} = # get actual device name
|
|
|
234 |
&getOneSNMPValue( $DEVICENAMEMIB,
|
|
|
235 |
$community,
|
|
|
236 |
$ip,
|
|
|
237 |
qr/= STRING: "?([^"]*)"?/);
|
|
|
238 |
|
|
|
239 |
$switchInfo->{$ip}->{'location'} = # and its snmp location
|
|
|
240 |
&getOneSNMPValue(
|
|
|
241 |
$DEVICELOCMIB,
|
|
|
242 |
$community,
|
|
|
243 |
$ip,
|
|
|
244 |
qr/= STRING: "?([^"]*)"?/ );
|
|
|
245 |
|
|
|
246 |
# get list of ports into hash with state as the value, '1' is operational
|
|
|
247 |
my $ports = &parseSNMPQuery( $SWITCHPORTMIB, $ip, $community, qr/$SWITCHPORTMIB\.(\d+)\s=\sINTEGER:\s+(\d+)/ );
|
|
|
248 |
print "=========Ports Hash\n" . Dumper( $ports ) . "\n" if $DEBUG == 4;
|
|
|
249 |
foreach my $port ( keys %$ports ) {
|
|
|
250 |
next unless $ports->{$port} == 1; # skip any non functional
|
|
|
251 |
next if $ignorePorts->{ $port };
|
|
|
252 |
# Get information about the ports. This may be possible to skip except on rare occassions?
|
|
|
253 |
# get port aliases, set to port number if undef
|
|
|
254 |
$switchInfo->{$ip}->{'ports'}->{$port}->{'alias'} =
|
|
|
255 |
&getOneSNMPValue(
|
|
|
256 |
"$SWITCHIFALIASMIB.$port",
|
|
|
257 |
$community,
|
|
|
258 |
$ip,
|
|
|
259 |
qr/\.\d+\s+=\s+STRING:\s+"?([^"]*)"?$/ );
|
|
|
260 |
$switchInfo->{$ip}->{'ports'}->{$port}->{'alias'} = $port unless defined ( $switchInfo->{$ip}->{'ports'}->{$port}->{'alias'} );
|
|
|
261 |
|
|
|
262 |
# get the port Description, set to port number if undef
|
|
|
263 |
$switchInfo->{$ip}->{'ports'}->{$port}->{'description'} =
|
|
|
264 |
&getOneSNMPValue(
|
|
|
265 |
"$SWITCHIFDESCMIB.$port",
|
|
|
266 |
$community,
|
|
|
267 |
$ip,
|
|
|
268 |
qr/\.\d+ = STRING: "?([^\"]*)"?$/ );
|
|
|
269 |
$switchInfo->{$ip}->{'ports'}->{$port}->{'description'} = $port unless defined ( $switchInfo->{$ip}->{'ports'}->{$port}->{'description'} );
|
|
|
270 |
}
|
136 |
rodolico |
271 |
return $switchInfo->{$ip};
|
133 |
rodolico |
272 |
}
|
|
|
273 |
|
|
|
274 |
# get all MAC addresses on switch and associate them with the correct port numbers
|
|
|
275 |
# https://www.cisco.com/c/en/us/support/docs/ip/simple-network-management-protocol-snmp/44800-mactoport44800.html
|
|
|
276 |
sub getSwitchPortMacs {
|
|
|
277 |
my ( $ip, $community, $ignoreList, $macList ) = @_;
|
|
|
278 |
print "\tFinding Mac Addresses\n" if $DEBUG > 1;
|
|
|
279 |
# get the MAC addresses into a list
|
|
|
280 |
my $macs = &parseSNMPQuery( $SWITCHMACMIB, $ip, $community, qr/$SWITCHMACMIB\.([0-9.]+)\s=\sHex-STRING:\s+([0-9a-z ]+)$/i );
|
|
|
281 |
print "Macs\n" . Dumper( $macs ) if $DEBUG >= 3;
|
|
|
282 |
# find which bridges they are on
|
|
|
283 |
print "\t\tFinding what bridges the MAC's are on\n" if $DEBUG > 2;
|
|
|
284 |
my $macBridges = &parseSNMPQuery( $BRIDGEPORTSMIB, $ip, $community, qr/$BRIDGEPORTSMIB\.([0-9.]+)\s=\sINTEGER:\s+(\d+)/ );
|
|
|
285 |
print "MacBridges\n" . Dumper( $macBridges ) if $DEBUG >= 3;
|
|
|
286 |
print "\t\tFinding what ports the ports are\n" if $DEBUG > 2;
|
|
|
287 |
my $bridgeToPort = &parseSNMPQuery( $BRIDGE2PORTMIB, $ip, $community, qr/$BRIDGE2PORTMIB\.([0-9.]+)\s=\sINTEGER:\s+(\d+)/ );
|
|
|
288 |
print "BridgeToPort\n" . Dumper( $bridgeToPort ) if $DEBUG > 3;
|
|
|
289 |
# now, merge the three together to figure out what port a particular mac is on
|
|
|
290 |
foreach my $mac ( keys %$macs ) {
|
|
|
291 |
print "\t\t\t$mac\n" if $DEBUG > 3;
|
|
|
292 |
next unless defined ( $macBridges->{$mac} ) and defined ($bridgeToPort->{$macBridges->{$mac}} );
|
|
|
293 |
my $port = $bridgeToPort->{$macBridges->{$mac}};
|
|
|
294 |
next if $ignoreList->{$port};
|
|
|
295 |
$macList->{&makeMAC($mac)}->{'switch'} = $ip;
|
|
|
296 |
$macList->{&makeMAC($mac)}->{'port'} = $port;
|
|
|
297 |
$macList->{&makeMAC($mac)}->{'originalMAC'} = $mac;
|
|
|
298 |
$macList->{&makeMAC($mac)}->{'lastseen'} = time();
|
|
|
299 |
}
|
|
|
300 |
}
|
|
|
301 |
|
136 |
rodolico |
302 |
# get IP address from router(s)
|
|
|
303 |
# since the information is already there, we grab the interface also
|
133 |
rodolico |
304 |
sub getRouterInfo {
|
|
|
305 |
my ( $ip, $community, $routerInfo ) = @_;
|
|
|
306 |
print "Working on $ip\n" if $DEBUG;
|
|
|
307 |
print "snmpwalk -v1 -c $community $ip $ROUTERIPMACMIB\n" if $DEBUG > 1;
|
|
|
308 |
my $values = `snmpwalk -v1 -c $community $ip $ROUTERIPMACMIB`;
|
|
|
309 |
my @lines = split( "\n", $values );
|
|
|
310 |
foreach my $line ( @lines ) {
|
|
|
311 |
$line =~ m/$ROUTERIPMACMIB\.([0-9]+)\.([0-9.]+)\s=\sHex-STRING:\s([0-9a-z ]+)/i;
|
|
|
312 |
my $interface = $1;
|
|
|
313 |
my $ip = $2;
|
|
|
314 |
my $mac = &cleanMac( $3 );
|
|
|
315 |
$routerInfo->{$mac}->{'ip'} = $ip;
|
|
|
316 |
$routerInfo->{$mac}->{'interface'} = $interface;
|
|
|
317 |
}
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
# load tab delimited values from $filename into hashref $nonRouterEntries
|
|
|
321 |
# format is
|
|
|
322 |
# macaddress
|
|
|
323 |
# 'ip' = IP Address
|
|
|
324 |
# 'mac' = MAC Address
|
|
|
325 |
# 'hostname' = HostName
|
|
|
326 |
|
|
|
327 |
sub loadNonRouterEntries {
|
136 |
rodolico |
328 |
my ( $scriptDir, $files, $staticMaps, $routerARP ) = @_;
|
|
|
329 |
# first, load the information from the files
|
|
|
330 |
foreach my $filename ( @$files ) {
|
|
|
331 |
open FN,"<$scriptDir/$filename" or die "Could not open $scriptDir/$filename: $!\n";
|
|
|
332 |
my $line = <FN>;
|
133 |
rodolico |
333 |
chomp $line;
|
136 |
rodolico |
334 |
my @headers = split( "\t", $line );
|
|
|
335 |
my $macIndex = 0;
|
|
|
336 |
while ( $headers[$macIndex] !~ 'mac' && $macIndex < @headers ) {
|
|
|
337 |
$macIndex++;
|
133 |
rodolico |
338 |
}
|
136 |
rodolico |
339 |
if ( $macIndex >= @headers ) {
|
|
|
340 |
warn "File $filename does not appear to contain a mac column\n";
|
|
|
341 |
return;
|
|
|
342 |
}
|
|
|
343 |
while ( $line = <FN> ) {
|
|
|
344 |
next if $line =~ m/^#/;
|
|
|
345 |
chomp $line;
|
|
|
346 |
my @values = split( "\t", $line );
|
|
|
347 |
my $mac = &cleanMac($values[$macIndex]);
|
|
|
348 |
for( my $i = 0; $i < @headers; $i++ ) {
|
|
|
349 |
# update the value if one does not already exist
|
164 |
rodolico |
350 |
if ( $values[$i] ) {
|
|
|
351 |
$routerARP->{$mac}->{$headers[$i]} = lc $values[$i] unless $routerARP->{$mac}->{$headers[$i]};
|
|
|
352 |
}
|
136 |
rodolico |
353 |
}
|
|
|
354 |
}
|
|
|
355 |
close FN;
|
133 |
rodolico |
356 |
}
|
136 |
rodolico |
357 |
# now, update from our staticmaps
|
|
|
358 |
foreach my $mac ( keys %$staticMaps ) {
|
|
|
359 |
# only update ip if it doesn't exist, or if we have an override
|
|
|
360 |
$routerARP->{$mac}->{'ip'} = $staticMaps->{$mac}->{'ip'} if $staticMaps->{$mac}->{'override'} || ! $routerARP->{$mac}->{'ip'};
|
|
|
361 |
# these don't exist in $routerARP, so we just add them
|
|
|
362 |
$routerARP->{$mac}->{'hostname'} = $staticMaps->{$mac}->{'hostname'};
|
|
|
363 |
$routerARP->{$mac}->{'override'} = $staticMaps->{$mac}->{'override'};
|
|
|
364 |
}
|
133 |
rodolico |
365 |
}
|
136 |
rodolico |
366 |
|
133 |
rodolico |
367 |
|
|
|
368 |
# merge nonRouterEntries into $routerARP, with $routerARP having precedence
|
|
|
369 |
sub updateRouterWithStatic {
|
|
|
370 |
my ( $routerARP, $nonRouterEntries ) = @_;
|
|
|
371 |
foreach my $mac ( keys %$nonRouterEntries ) {
|
|
|
372 |
# assign the hostname from staticmaps if there is one, and either override is true, or routerARP doesn't have one
|
|
|
373 |
$routerARP->{$mac}->{'hostname'} = $nonRouterEntries->{$mac}->{'hostname'}
|
|
|
374 |
if ( $nonRouterEntries->{$mac}->{'hostname'}
|
|
|
375 |
&& (
|
|
|
376 |
! $routerARP->{$mac}->{'hostname'}
|
|
|
377 |
|| $nonRouterEntries->{$mac}->{'override'}
|
|
|
378 |
)
|
|
|
379 |
);
|
|
|
380 |
# assign the hostname from staticmaps if there is one, and either override is true, or routerARP doesn't have one
|
|
|
381 |
$routerARP->{$mac}->{'ip'} = $nonRouterEntries->{$mac}->{'ip'}
|
|
|
382 |
if ( $nonRouterEntries->{$mac}->{'ip'}
|
|
|
383 |
&& (
|
|
|
384 |
! $routerARP->{$mac}->{'ip'}
|
|
|
385 |
|| $nonRouterEntries->{$mac}->{'override'}
|
|
|
386 |
)
|
|
|
387 |
);
|
|
|
388 |
}
|
|
|
389 |
}
|
|
|
390 |
|
136 |
rodolico |
391 |
# get IP addresses from the list of them we have
|
|
|
392 |
sub getIPAddresses {
|
|
|
393 |
my ( $macList, $ipList ) = @_;
|
|
|
394 |
foreach my $mac ( keys %$macList ) {
|
|
|
395 |
$macList->{$mac}->{'ip'} = $ipList->{$mac}->{'ip'} if $ipList->{$mac}->{'ip'};
|
|
|
396 |
}
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
# grab names by reverse DNS from the IP addresses
|
|
|
400 |
# if that fails, or if there is an override
|
|
|
401 |
# use the name from $routerARP, if it exists;
|
|
|
402 |
sub getDNSNames {
|
|
|
403 |
my ( $macList, $dnsResource, $routerARP ) = @_;
|
|
|
404 |
foreach my $mac ( keys %$macList ) {
|
|
|
405 |
if ( $routerARP->{$mac}->{'override'} && $routerARP->{$mac}->{'hostname'} ) {
|
|
|
406 |
$macList->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'};
|
|
|
407 |
} else { # try a DNS lookup
|
|
|
408 |
$macList->{$mac}->{'hostname'} = &getHostName( $dnsResource, $macList->{$mac}->{'ip'} );
|
|
|
409 |
# still didn't find one, so add one from $routerARP if it exists
|
|
|
410 |
$macList->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'} if ! $macList->{$mac}->{'hostname'} && $routerARP->{$mac}->{'hostname'};
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
}
|
|
|
414 |
|
157 |
rodolico |
415 |
# As a last ditch effort, we will use dig (if available) to try an get rdns entries
|
|
|
416 |
# for the IP's we've found.
|
|
|
417 |
sub doDig {
|
|
|
418 |
return unless `which dig`;
|
|
|
419 |
my $macList = shift;
|
|
|
420 |
my @dnsServers = @_;
|
|
|
421 |
foreach my $mac ( keys %$macList ) {
|
|
|
422 |
next if $macList->{$mac}->{'hostname'}; # we already have a name
|
|
|
423 |
next unless $macList->{$mac}->{'ip'}; # can't process if we have no IP
|
|
|
424 |
foreach my $ns ( @dnsServers ) {
|
|
|
425 |
my $name = `dig \@$ns -x $macList->{$mac}->{ip} +short`;
|
|
|
426 |
chomp $name;
|
|
|
427 |
if ( $name ) {
|
|
|
428 |
$macList->{$mac}->{'hostname'} = $name;
|
|
|
429 |
last;
|
|
|
430 |
}
|
|
|
431 |
}
|
|
|
432 |
}
|
|
|
433 |
}
|
|
|
434 |
|
136 |
rodolico |
435 |
# given a value of a number follwed by a unit (ie, 2w, 12h, 3m), determine
|
|
|
436 |
# the unixtime for that long ago.
|
|
|
437 |
sub calcAge {
|
|
|
438 |
my $time = shift;
|
|
|
439 |
my %seconds = (
|
|
|
440 |
's' => 1,
|
|
|
441 |
'h' => 60*60,
|
|
|
442 |
'd' => 86400,
|
|
|
443 |
'w' => 7*86400,
|
|
|
444 |
'm' => 7*30.5*86400,
|
|
|
445 |
'y' => 86400*365.2425
|
|
|
446 |
);
|
|
|
447 |
$time = lc $time;
|
|
|
448 |
$time =~ m/^(\d+)(.*)/;
|
|
|
449 |
$time = $1;
|
|
|
450 |
my $unit = $2;
|
|
|
451 |
$unit = 's' unless $unit;
|
|
|
452 |
return defined( $seconds{$unit} ) ? $time * $seconds{$unit} : 0;
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
# given an integer/modifier (1w, 3d, 10h), remove all mac entries which have not
|
|
|
456 |
# been seen since then.
|
|
|
457 |
sub cleanUpOldEntries {
|
|
|
458 |
my ( $ttl, $list ) = @_;
|
|
|
459 |
my $currentTime = time;
|
|
|
460 |
my $deleteBefore = &calcAge( $ttl );
|
|
|
461 |
if ( $deleteBefore == 0 ) {
|
|
|
462 |
warn "Invalid ttl definition [$ttl], not cleaning up\n";
|
|
|
463 |
return;
|
|
|
464 |
}
|
|
|
465 |
$deleteBefore = time - $deleteBefore;
|
|
|
466 |
foreach my $mac ( keys %{ $list->{'macList'} } ) {
|
|
|
467 |
if ( $list->{'macList'}->{$mac}->{'lastseen'} < $deleteBefore ) {
|
|
|
468 |
# print
|
|
|
469 |
# "Deleting $mac with a last seen of " .
|
|
|
470 |
# $list->{'macList'}->{$mac}->{'lastseen'} .
|
|
|
471 |
# ", current time is $currentTime for a difference of " .
|
|
|
472 |
# ( $currentTime - $list->{'macList'}->{$mac}->{'lastseen'} ) .
|
|
|
473 |
# "\n";
|
|
|
474 |
delete $list->{'macList'}->{$mac} if $list->{'macList'}->{$mac}->{'lastseen'};
|
|
|
475 |
}
|
|
|
476 |
}
|
|
|
477 |
}
|
|
|
478 |
|
133 |
rodolico |
479 |
################################################################################
|
|
|
480 |
# Main
|
|
|
481 |
################################################################################
|
|
|
482 |
|
|
|
483 |
# handle any command line parameters that may have been passed in
|
|
|
484 |
my $version = 0; # just used to determine if we should display the version
|
|
|
485 |
my $help = 0; # also if we want help
|
|
|
486 |
my $nosnmp = 0; # we do NOT want to run snmp commands
|
136 |
rodolico |
487 |
my $forceRefres = 0; # force a refresh even if one is not called for now
|
133 |
rodolico |
488 |
GetOptions (
|
|
|
489 |
'debug|d=i' => \$DEBUG,
|
|
|
490 |
'nosnmp|n' => \$nosnmp,
|
136 |
rodolico |
491 |
'refresh|r' => \$forceRefres,
|
133 |
rodolico |
492 |
'help|h' => \$help,
|
|
|
493 |
'version|v' => \$version,
|
|
|
494 |
) or die "Error parsing command line\n";
|
|
|
495 |
|
|
|
496 |
|
|
|
497 |
if ( $help ) { &help() ; exit; }
|
|
|
498 |
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
|
|
|
499 |
|
|
|
500 |
# Get configuration file into $config
|
|
|
501 |
my $config = &loadConfig( $CONFIGFILE );
|
136 |
rodolico |
502 |
print Dumper( $config ) if $DEBUG > 1; die if $DEBUG > 4;
|
133 |
rodolico |
503 |
|
136 |
rodolico |
504 |
# load the savefile, if it exists
|
|
|
505 |
my $saveFile = { 'macList' => {}, 'switchInfo' => {} };
|
|
|
506 |
# read the saved state into memory if it exists
|
|
|
507 |
if ( -e $STATEFILE ) {
|
|
|
508 |
my $yaml = YAML::Tiny->read( $STATEFILE );
|
|
|
509 |
$saveFile = \%{ $yaml->[0] };
|
|
|
510 |
}
|
134 |
rodolico |
511 |
|
133 |
rodolico |
512 |
|
138 |
rodolico |
513 |
my $dnsResource = 0;
|
139 |
rodolico |
514 |
eval "use Net::DNS;"; # dns lookup, apt install libnet-dns-perl
|
|
|
515 |
if ( $@ ) {
|
|
|
516 |
$dnsResource = 0;
|
|
|
517 |
} else {
|
138 |
rodolico |
518 |
$dnsResource = [];
|
|
|
519 |
# set up one dns resource for every router we have
|
|
|
520 |
# too many failures when trying to define both of them at the same time
|
|
|
521 |
foreach my $router ( keys %{ $config->{'routers'} } ) {
|
|
|
522 |
push @$dnsResource, Net::DNS::Resolver->new(
|
|
|
523 |
'nameservers' => [ $router ],
|
|
|
524 |
'recurse' => 0
|
|
|
525 |
);
|
|
|
526 |
}
|
|
|
527 |
};
|
|
|
528 |
|
136 |
rodolico |
529 |
my $refresh = 1; # determines if we get extra information like switch info, port aliases, etc...
|
|
|
530 |
if ( defined( $config->{'refresh'} ) ) {
|
|
|
531 |
# get the number of seconds between refreshes
|
|
|
532 |
$refresh = &calcAge( $config->{'refresh'} );
|
|
|
533 |
# if we have not recorded a refresh, set the refresh time to run now
|
|
|
534 |
$saveFile->{'lastrefresh'} = time - &calcAge( $config->{'refresh'} ) - 1
|
|
|
535 |
unless defined $saveFile->{'lastrefresh'};
|
|
|
536 |
$refresh = $saveFile->{'lastrefresh'} + &calcAge( $config->{'refresh'} ) < time || $forceRefres;
|
|
|
537 |
$saveFile->{'lastrefresh'} = time if $refresh;
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
print STDERR "Doing a full refresh\n" if $refresh && $DEBUG > 1;
|
|
|
541 |
|
|
|
542 |
|
|
|
543 |
|
133 |
rodolico |
544 |
# load the information for the switches into $switchInfo.
|
|
|
545 |
foreach my $switch ( keys %{$config->{'switches'}} ) {
|
|
|
546 |
last if $nosnmp; # do not read switches if $nosnmp set
|
136 |
rodolico |
547 |
my $ignoreList = { map { $_ => 1 } @{ $config->{'switches'}->{$switch}->{'portsToIgnore'} } }; # hash of ports to ignore
|
|
|
548 |
#my $ignoreList = \%ignoreList;
|
133 |
rodolico |
549 |
print "ignoreList for $switch\n" . Dumper ($ignoreList) if $DEBUG > 2;
|
|
|
550 |
# just get basic switch information, inlcuding a list of all ports which are active and not in portsToIgnore
|
136 |
rodolico |
551 |
$saveFile->{'switchInfo'}->{$switch} = &getSwitchInfo( $switch, $config->{'switches'}->{$switch}->{'community'}, $ignoreList ) if $refresh;
|
133 |
rodolico |
552 |
# get all the MAC's on this switch
|
136 |
rodolico |
553 |
&getSwitchPortMacs( $switch, $config->{'switches'}->{$switch}->{'community'}, $ignoreList, $saveFile->{'macList'} );
|
133 |
rodolico |
554 |
}
|
|
|
555 |
|
136 |
rodolico |
556 |
|
133 |
rodolico |
557 |
my $routerARP = {};
|
|
|
558 |
# Read the ARP table from the router(s)
|
|
|
559 |
foreach my $router ( keys %{$config->{'routers'}} ) {
|
136 |
rodolico |
560 |
#last if $nosnmp; # do not read routers if $nosnmp set
|
|
|
561 |
&getRouterInfo( $router, $config->{'routers'}->{$router}->{'community'}, $routerARP ) if $refresh;
|
133 |
rodolico |
562 |
}
|
|
|
563 |
print "Routers\n" . Dumper ($routerARP ) if $DEBUG > 2;
|
136 |
rodolico |
564 |
# add in anything we have defined in files or in staticmaps in the config file
|
|
|
565 |
&loadNonRouterEntries( $scriptDir, $config->{'nonrouter'}, $config->{'staticmaps'}, $routerARP );
|
|
|
566 |
print "Routers after loadNonRouterEntries\n" . Dumper ($routerARP ) if $DEBUG > 2;
|
|
|
567 |
# populate the mac list with information from $routerARP
|
|
|
568 |
&getIPAddresses( $saveFile->{'macList'}, $routerARP );
|
|
|
569 |
print "Routers after getIPAddresses\n" . Dumper ($routerARP ) if $DEBUG > 2;
|
|
|
570 |
# get the DNS names
|
|
|
571 |
&getDNSNames( $saveFile->{'macList'}, $dnsResource, $routerARP ) if $refresh;
|
|
|
572 |
print "Routers after getDNSNames\n" . Dumper ($routerARP ) if $DEBUG > 2;
|
157 |
rodolico |
573 |
&doDig( $saveFile->{'macList'}, keys %{$config->{'routers'}} );
|
136 |
rodolico |
574 |
# clean up any old entries
|
|
|
575 |
&cleanUpOldEntries( $config->{'ttl'}, $saveFile ) if $config->{'ttl'} && $refresh;
|
133 |
rodolico |
576 |
# save the file
|
|
|
577 |
my $yaml = YAML::Tiny->new( $saveFile );
|
|
|
578 |
$yaml->write( $STATEFILE );
|
|
|
579 |
|
|
|
580 |
|
|
|
581 |
1;
|
|
|
582 |
|