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

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 106 CC 6 SCAR 22.3
CC
6
Cov
0%
arpnip
20.0
macsuck
12.0
BEGIN
2.0
CRAP 9.3
1 package App::Netdisco::SSHCollector::Platform::OS10;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::OS10
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP entries from Dell OS10 devices.
10  
11 =cut
12  
13 use strict;
14 use warnings;
15  
16 use Dancer ':script';
17 use Moo;
18 use Expect;
19 use NetAddr::MAC qw/mac_as_ieee/;
20 use Regexp::Common 'net';
21  
22 =head1 PUBLIC METHODS
23  
24 =over 4
25  
26 =item B<arpnip($host, $ssh)>
27  
28 Retrieve ARP entries from device. C<$host> is the hostname or IP address
29 of the device. C<$ssh> is a Net::OpenSSH connection to the device.
30  
31 Returns a list of hashrefs in the format C<< { mac => MACADDR, ip => IPADDR } >>.
32  
33 =back
34  
35 =cut
36  
37 my $if_name_map = {
38 Vl => "Vlan",
39 Lo => "Loopback",
40 Eth => "Ethernet",
41 Po => "Port-channel",
42 };
43  
44 sub arpnip {
45 my ($self, $hostlabel, $ssh, $args) = @_;
46  
47 debug "$hostlabel $$ arpnip()";
48  
49 my ($pty, $pid) = $ssh->open2pty;
50 unless ($pty) {
51 warn "unable to run remote command [$hostlabel] " . $ssh->error;
52 return ();
53 }
54  
55 # $Expect::Debug = 1;
56 # $Expect::Exp_Internal = 1;
57  
58 my $expect = Expect->init($pty);
59 $expect->raw_pty(1);
60  
61 my ($pos, $error, $match, $before, $after);
62 my $prompt = qr/# +$/;
63 my $timeout = 60;
64  
65 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
66  
67 # Get all VRFs, skip header line
68 $expect->send("show ip vrf | except VRF-Name | no-more \n");
69 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
70 my @vrfs = split(/\R/, $before);
71  
72 # Regex for VRF name
73 my $vrf_re = qr/^([a-z\-_0-9\.]+)\s+.+$/i;
74  
75 # IP ARP match
76 my $iparp_re = qr/^((\d{1,3}\.){3}\d{1,3})\s*(([0-9a-f]{2}[:-]){5}[0-9a-f]{2})\s+.+$/i;
77  
78 # Will hold results
79 my @arpentries;
80  
81 foreach my $vrf_line (@vrfs) {
82 my $vrf_name ;
83 # Get the VRF name
84 if ($vrf_line && $vrf_line =~ m/$vrf_re/) {
85 $vrf_name = $1;
86 } else {
87 next ;
88 }
89 # Get IP ARP entries for this VRF
90 my $vrf_cmd = sprintf("show ip arp vrf %s | no-more \n", $vrf_name) ;
91 $expect->send($vrf_cmd);
92 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
93 my @iparps = split(/\R/, $before);
94  
95 foreach my $iparp_line (@iparps) {
96 if($iparp_line && $iparp_line =~ m/$iparp_re/) {
97 push(@arpentries, { ip => $1, mac => $3 });
98 }
99 }
100 }
101  
102 return @arpentries;
103 }
104  
105 sub macsuck {
106 my ($self, $hostlabel, $ssh, $args) = @_;
107  
108 debug "$hostlabel $$ macsuck()";
109 my $cmds = <<EOF;
110 show mac address-table | no-more
111 EOF
112 my @data = $ssh->capture({stdin_data => $cmds}); chomp @data;
113 if ($ssh->error) {
114 info "$hostlabel $$ error in SSH command " . $ssh->error;
115 return
116 }
117  
118 #hostname# show mac address-table
119 #Legend:
120 # VlanId Mac Address Type Interface
121 # 54 00:00:5e:00:01:36 dynamic port-channel1
122 # 54 00:50:56:af:12:f5 dynamic port-channel1
123 # 54 00:50:56:af:ca:a3 dynamic port-channel1
124 # 54 04:09:73:e3:22:40 dynamic port-channel17
125
126 my $re_mac_line = qr/^(\d+)\s+((([0-9a-f]{2}[:-]){5})[0-9a-f]{2})\s+\w+\s+([a-z]+.*)$/i;
127 my $macentries = {};
128  
129 foreach my $line (@data) {
130 if ($line && $line =~ m/$re_mac_line/) {
131 my $port = $5;
132 my $vlan = $1;
133  
134 ++$macentries->{$vlan}->{$port}->{mac_as_ieee($2)};
135 }
136 }
137  
138 return $macentries;
139 }
140  
141 1;
142  
143