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

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 52 CC 3 SCAR 17.5
CC
3
Cov
0%
arpnip
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.8
1 package App::Netdisco::SSHCollector::Platform::ArubaCX;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::ArubaCX
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP entries from ArubaCX devices
10  
11 device_auth:
12 - tag: ssharubacx
13 driver: cli
14 platform: ArubaCX
15 only: '192.0.2.1'
16 username: oliver
17 password: letmein
18  
19 =cut
20  
21 use strict;
22 use warnings;
23  
24 use Dancer ':script';
25 use Expect;
26 use Moo;
27  
28 =head1 PUBLIC METHODS
29  
30 =over 4
31  
32 =item B<arpnip($host, $ssh)>
33  
34 Retrieve ARP entries from device. C<$host> is the hostname or IP address
35 of the device. C<$ssh> is a Net::OpenSSH connection to the device.
36  
37 Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>.
38  
39 =back
40  
41 =cut
42  
43 sub arpnip {
44  
45 my ($self, $hostlabel, $ssh, $args) = @_;
46  
47 debug "$hostlabel $$ arpnip()";
48  
49 my ($pty, $pid) = $ssh->open2pty;
50 unless ($pty) {
51 debug "unable to run remote command [$hostlabel] " . $ssh->error;
52 return ();
53 }
54 my $expect = Expect->init($pty);
55  
56 my ($pos, $error, $match, $before, $after);
57  
58 my $prompt = qr/#/;
59 ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt);
60  
61 $expect->send("no page\n");
62 ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt);
63  
64 $expect->send("show arp all-vrfs\n");
65 ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt);
66 my @lines= split(m/\n/,$before);
67  
68 $expect->send("exit\n");
69 $expect->soft_close();
70  
71 my @arpentries = ();
72  
73 # Example output from 'show arp':
74 #
75 # IPv4 Address MAC Port Physical Port State
76 # -------------------------------------------------------------------------------------------------------------
77 # a.b.c.d aa:bb:cc:dd:ee:ff vlanNN 1/1/1 reachable
78 # ...
79 #
80 # Total Number Of ARP Entries Listed: 573.
81 # -------------------------------------------------------------------------------------------------------------
82  
83 # pattern of the lines we are interested in:
84 my $ip_patt = qr/(?:\d+\.\d+\.\d+\.\d+)/x;
85 my $mac_patt = qr/(?:[0-9a-f]{2}:){5}[0-9a-f]{2}/x;
86 my $linereg = qr/($ip_patt)\s+($mac_patt)\s+\S+\s+\S+/x;
87  
88 foreach my $line (@lines) {
89 if ($line =~ $linereg) {
90 my ($ip, $mac) = ($1, $2);
91 push @arpentries, { mac => $mac, ip => $ip };
92 }
93 }
94  
95 return @arpentries;
96 }
97  
98 1;