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
|
#!/usr/bin/perl
use warnings;
use strict;
use lib '../include';
use nms qw(switch_connect switch_exec switch_disconnect);
use Net::Telnet::Cisco;
BEGIN {
require "../include/config.pm";
eval {
require "../include/config.local.pm";
};
}
#
# Fetch list of MAC addresses and IPv6 addresses
#
sub query_router {
my ($host) = @_;
my $ios = Net::Telnet::Cisco->new(
Host => $host,
Errmode => 'return');
if (not defined $ios) {
warn "Can't connect to $host: $!, skipping";
return ();
}
if (not $ios->login($nms::config::ios_user, $nms::config::ios_pass)) {
warn "Can't login to $host. Wrong username or password?";
return ();
}
$ios->autopage(0);
$ios->cmd("terminal length 0");
my @v6data = $ios->cmd('show ipv6 neighbors')
or warn "$host wouldn't let me run show ipv6 neighbors.";
my @v4data = $ios->cmd('show ip arp')
or warn "$host wouldn't let me run show ip arp.";
# Remove useless header and footer
# shift @v6data;
pop @v6data;
# shift @v4data;
pop @v4data;
return { 'v6' => \@v6data, 'v4' => \@v4data };
}
while (1) {
print "Gathering IPv6 and IPv4 stats\n";
# Connect to DB
my $dbh = nms::db_connect();
$dbh->{AutoCommit} = 0;
my ($v4, $v6) = 0;
foreach my $router (@nms::config::distrobox_ips) {
my $data = query_router($router);
# IPv6
foreach my $line (@{$data->{'v6'}}) {
my ($address, $age, $mac, undef, undef) = split('\s+', $line);
if ($mac =~ /[a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}/ && # Sanity check MAC address
$address !~ /^FE.*/) { # Filter out non-routable addresses
my $q = $dbh->prepare('INSERT INTO ipv6 (address, age, mac, time) VALUES (?, ?, ?, timeofday()::timestamp)')
or die "Can't prepare query: $!";
$q->execute($address, $age, $mac)
or die "Can't execute query: $!";
$v6++;
}
}
# IPv4
foreach my $line (@{$data->{'v4'}}) {
my (undef, $address, $age, $mac, undef, undef) = split('\s+', $line);
if ($mac =~ /[a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}/) {# Sanity check MAC address
$age = 0 if $age eq '-';
my $q = $dbh->prepare('INSERT INTO ipv4 (address, age, mac, time) VALUES (?, ?, ?, timeofday()::timestamp)')
or die "Can't prepare query: $!";
$q->execute($address, $age, $mac)
or die "Can't execute query: $!";
$v4++;
}
}
}
print "Added $v6 IPv6 addresses and $v4 IPv4 addresses.\n";
$dbh->commit;
$dbh->disconnect;
print "Sleeping for two minutes.\n";
sleep 120; # Sleep for two minutes
}
|