1package App::Netdisco::SSHCollector::Platform::NXOS;
2
3=head1 NAME
4
5App::Netdisco::SSHCollector::Platform::NXOS
6
7=head1 DESCRIPTION
8
9Collect ARP entries from Cisco NXOS devices.
10
11=cut
12
13use strict;
14use warnings;
15
16use Dancer ':script';
17use Moo;
18use Expect;
19
20=head1 PUBLIC METHODS
21
22=over 4
23
24=item B<arpnip($host, $ssh)>
25
26Retrieve ARP entries from device. C<$host> is the hostname or IP address
27of the device. C<$ssh> is a Net::OpenSSH connection to the device.
28
29Returns a list of hashrefs in the format C<< { mac => MACADDR, ip => IPADDR } >>.
30
31=back
32
33=cut
34
35sub arpnip {
36    my ($self, $hostlabel, $ssh, $args) = @_;
37
38    debug "$hostlabel $$ arpnip()";
39
40    my ($pty, $pid) = $ssh->open2pty;
41    unless ($pty) {
42        warn "unable to run remote command [$hostlabel] " . $ssh->error;
43        return ();
44    }
45
46    #$Expect::Debug = 1;
47    #$Expect::Exp_Internal = 1;
48
49    my $expect = Expect->init($pty);
50    $expect->raw_pty(1);
51
52    my ($pos, $error, $match, $before, $after);
53    my $prompt = qr/# +$/;
54    my $timeout = 10;
55
56    ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
57
58    $expect->send("terminal length 0\n");
59    ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
60
61    # we filter on the : in Age as the output header of the command may contain prompt chars, e.g.
62    # Flags:   # - Adjacencies Throttled for Glean
63    $expect->send("show ip arp vrf all | inc :\n");
64    ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
65
66    my @arpentries;
67    my @data = split(/\R/, $before);
68
69    #IP ARP Table for all contexts
70    #Total number of entries: 5
71    #Address         Age       MAC Address     Interface
72    #192.168.228.1   00:00:43  0000.abcd.1111  mgmt0
73    #192.168.228.9   00:05:24  cccc.7777.1b1b  mgmt0
74
75    foreach (@data) {
76        my ($ip, $age, $mac, $iface) = split(/\s+/);
77
78        if ($ip && $ip =~ m/(\d{1,3}\.){3}\d{1,3}/
79            && $mac =~ m/([0-9a-f]{4}\.){2}[0-9a-f]{4}/i) {
80              push(@arpentries, { ip => $ip, mac => $mac });
81        }
82    }
83
84    return @arpentries;
85}
86
871;
88
89