blib/lib/App/Netdisco/SSHCollector/Platform/IOS.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 75 CC 6 SCAR 26.6
CC
6
Cov
0%
macsuck
30.0
arpnip
6.0
BEGIN
2.0
CRAP 14.3
1 package App::Netdisco::SSHCollector::Platform::IOS;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::IOS
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP entries from Cisco IOS devices.
10  
11 =cut
12  
13 use strict;
14 use warnings;
15  
16 use Dancer ':script';
17 use NetAddr::MAC qw/mac_as_ieee/;
18 use Moo;
19  
20 =head1 PUBLIC METHODS
21  
22 =over 4
23  
24 =item B<arpnip($host, $ssh)>
25  
26 Retrieve ARP entries from device. C<$host> is the hostname or IP address
27 of the device. C<$ssh> is a Net::OpenSSH connection to the device.
28  
29 Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>.
30  
31 =back
32  
33 =cut
34  
35 my $if_name_map = {
36 Vl => "Vlan",
37 Lo => "Loopback",
38 Fa => "FastEthernet",
39 Gi => "GigabitEthernet",
40 Tw => "TwoGigabitEthernet",
41 Fi => "FiveGigabitEthernet",
42 Te => "TenGigabitEthernet",
43 Twe => "TwentyFiveGigE",
44 Fo => "FortyGigabitEthernet",
45 Hu => "HundredGigE",
46 Po => "Port-channel",
47 Bl => "Bluetooth",
48 };
49  
50 sub arpnip {
51 my ($self, $hostlabel, $ssh, $args) = @_;
52  
53 debug "$hostlabel $$ arpnip()";
54 my @data = $ssh->capture("show ip arp");
55  
56 chomp @data;
57 my @arpentries;
58  
59 # Internet 172.16.20.15 13 0024.b269.867d ARPA FastEthernet0/0.1
60 foreach my $line (@data) {
61 next unless $line =~ m/^Internet/;
62 my @fields = split m/\s+/, $line;
63  
64 push @arpentries, { mac => $fields[3], ip => $fields[1] };
65 }
66  
67 return @arpentries;
68 }
69  
70 sub macsuck {
71 my ($self, $hostlabel, $ssh, $args) = @_;
72  
73 debug "$hostlabel $$ macsuck()";
74 my $cmds = <<EOF;
75 terminal length 0
76 show mac address-table
77 EOF
78 my @data = $ssh->capture({stdin_data => $cmds}); chomp @data;
79 if ($ssh->error) {
80 info "$hostlabel $$ error in SSH command " . $ssh->error;
81 return;
82 }
83  
84 #hostname#sh mac address-table
85 # Mac Address Table
86 #-------------------------------------------
87 #
88 #Vlan Mac Address Type Ports
89 #---- ----------- -------- -----
90 # All 0100.0ccc.cccc STATIC CPU
91 # 10 xxxx.7fc7.xxxx DYNAMIC Gi0/1/0
92 # 10 xxxx.027c.xxxx STATIC CPU
93  
94 my $re_mac_line = qr/^\s*(All|[0-9]+)\s+([0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4})\s+\S+\s+([a-zA-Z]+)([0-9\/\.]*)/i;
95 my $macentries = {};
96  
97 foreach my $line (@data) {
98 if ($line && $line =~ m/$re_mac_line/) {
99 my $port = sprintf '%s%s', ($if_name_map->{$3} || $3), ($4 || '');
100 my $vlan = ($1 ? ($1 eq 'All' ? 0 : $1) : 0);
101  
102 ++$macentries->{$vlan}->{$port}->{mac_as_ieee($2)};
103 }
104 }
105  
106 return $macentries;
107 }
108  
109 1;