-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-mac-timestamps
executable file
·196 lines (159 loc) · 4.97 KB
/
update-mac-timestamps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to update mac_addresses_ts on hosts in the database based on output of
# cisco command
#
# terminal length 0
# sh ip arp
#
use strict;
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_q $opt_r);
getopts ('hdqr:');
my $debug = defined ($opt_d);
my $quiet = defined ($opt_q);
my $rename_suffix = $opt_r;
my @files = @ARGV;
my $default_ttl;
if ($#files == -1 or defined ($opt_h)) {
die (<<EOM);
Syntax: $0 [options] [cisco-output-file ...]\n
options :\n
-d debug
-q quiet
-r suffix rename files if successfull, append suffix
EOM
}
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
print ("*** Parsing " . ($#files + 1) . " files\n") unless ($quiet);
my %data;
parse_files ($hostdb, \%data, $debug, $quiet, @files);
print ("*** Updating database\n") if (! $quiet);
update_db ($hostdb, \%data, $debug, $quiet);
if (defined ($rename_suffix)) {
print ("*** Renaming " . ($#files + 1) . " files (suffix $rename_suffix)\n") unless ($quiet);
rename_files ($rename_suffix, @files);
}
exit (0);
sub parse_files
{
my $hostdb = shift;
my $data = shift;
my $debug = shift;
my $quiet = shift;
my @files = @_;
foreach my $file (@files) {
chomp ($file);
open (FIL, "< $file") or warn ("$0: Could not open $file for reading: $!\n"), next;
my $ts = (stat ($file))[9];
while (my $rad = <FIL>) {
chomp ($rad);
my ($ip, $mac) = ('', '');
if ($rad =~ /^Internet\s+([0-9\.]+)\s+(\d|\-)+\s+([0-9a-f\.]+)\s+/) {
# Cisco 'show ip arp' output, example :
# Protocol Address Age (min) Hardware Addr Type Interface
# Internet 10.11.0.1 - 0023.9bcd.45f9 ARPA FastEthernet0/1
$ip = $1;
$mac = $3;
} elsif ($rad =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([\da-f:]+)$/) {
# Universal key-value format : IPv4<white space>MAC
# example :
# 192.0.2.11 00:01:02:03:04:05:06
$ip = $1;
$mac = $2;
} else {
# unknown line, not considered an error in this script - the input data is
# allowed to be the full output of a 'telnet' command fetching the data
warn ("Ignoring unknown data on line $. : $rad\n") if ($debug);
next;
}
warn ("IP $ip MAC $mac TS $ts\n") if ($debug);
warn ("Invalid MAC address '$mac' on $file:$.\n"), next unless $hostdb->clean_mac_address ($mac);
$data{"$ip;$mac"} = $ts if (! defined ($data{"$ip;$mac"}) or
$ts > $data{"$ip;$mac"});
}
close (FIL);
print ("$file\n") unless ($quiet);
}
}
sub update_db
{
my $hostdb = shift;
my $data = shift;
my $debug = shift;
my $quiet = shift;
my $ip_mac;
foreach $ip_mac (keys %$data) {
my ($ip, $mac) = split (";", $ip_mac);
warn ("IP $ip MAC $mac\n") if ($debug);
my @hosts = $hostdb->findhostbyip ($ip);
if (! @hosts) {
warn ("No host with IP '$ip' in database\n") unless ($quiet);
next;
}
my $update_partof_hosts = 1;
if (scalar @hosts > 0) {
# More than one host with this IP found, only update those that are
# not partof another host. Ideally, we would just eliminate childs
# whose parent is also in @hosts, but that logic is more complex.
# This is probably good enough.
$update_partof_hosts = 0;
}
foreach my $host (@hosts) {
my $id = $host->id ();
my $host_mac = $host->mac_address ();
if ($host->dhcpmode () eq 'DYNAMIC') {
if (defined ($host_mac)) {
my $valid = 1;
$host->mac_address ('NULL') or warn ("Failed to clear MAC address on dynamic host $id (IP $ip): $host->{error}\n"), $valid = 0;
$host->commit () if ($valid);
warn ("Cleared MAC address on dynamic host $id (IP $ip)\n") if ($debug and $valid);
}
next;
}
my $part_of = $host->partof () || '';
if ($part_of and ! $update_partof_hosts) {
warn ("Skipping $ip ($mac) host $id which is part of another host ($part_of)\n") if ($debug);
next;
}
if (! defined ($host_mac)) {
# no mac in db, put this one there
$host->mac_address ($mac);
$host_mac = $mac;
}
if ($host_mac eq $mac) {
my $host_mac_ts = $host->unix_mac_address_ts ();
my $ts = $data{$ip_mac};
if (! defined ($host_mac_ts) or
($host_mac_ts < $ts)) {
my $valid = 1;
$host->mac_address_ts ("unixtime:$ts") or warn ("Failed to set TS on $ip: $host->{error}\n"), $valid = 0;
$host->commit () if ($valid);
warn ("Set new timestamp on $ip\n") if ($debug and $valid);
} else {
if ($host_mac_ts > $ts) {
# only warn for greater than, not equal
warn ("Database timestamp on $ip ($host_mac_ts) greater than this: $ts (+" .
($host_mac_ts - $ts) . " seconds)\n") unless ($quiet);
}
}
} else {
warn ("Unexpected mac-address on $ip: $mac (db has $host_mac) - ignoring\n") unless ($quiet);
}
}
}
}
sub rename_files
{
my $suffix = shift;
my @files = @_;
my $t;
foreach $t (@files) {
rename ($t, "${t}${suffix}") or warn ("$0: Could not rename '$t' to '${t}${suffix}': $!\n");
}
}