1package App::Netdisco::SSHCollector::Platform::IOSXR;
2
3=head1 NAME
4
5App::Netdisco::SSHCollector::Platform::IOSXR
6
7=head1 DESCRIPTION
8
9Collect ARP entries from IOSXR routers using Expect
10
11This is a reworked version of the IOSXR module, and it is suitable
12for both 32- and 64-bit IOSXR.
13
14=cut
15
16use strict;
17use warnings;
18
19use Dancer ':script';
20use Expect;
21use Moo;
22
23=head1 PUBLIC METHODS
24
25=over 4
26
27=item B<arpnip($host, $ssh)>
28
29Retrieve ARP entries from device. C<$host> is the hostname or IP address
30of the device. C<$ssh> is a Net::OpenSSH connection to the device.
31
32Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>.
33
34=back
35
36=cut
37
38sub arpnip {
39    my ($self, $hostlabel, $ssh, $args) = @_;
40
41    debug "$hostlabel $$ arpnip()";
42
43    my ($pty, $pid) = $ssh->open2pty;
44    unless ($pty) {
45        warn "unable to run remote command [$hostlabel] " . $ssh->error;
46        return ();
47    }
48    my $expect = Expect->init($pty);
49
50    my ($pos, $error, $match, $before, $after);
51    my $prompt = qr/# +$/;
52    my $timeout = 10;
53
54    ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
55
56    $expect->send("terminal length 0\n");
57    ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
58
59    $expect->send("show arp vrf all\n");
60    ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
61
62    my @arpentries = ();
63    my @data = split(m/\n/, $before);
64
65    foreach (@data) {
66        my ($ip, $age, $mac, $state, $t, $iface) = split(/\s+/);
67
68        if ($ip =~ m/(\d{1,3}\.){3}\d{1,3}/
69            && $mac =~ m/([0-9a-f]{4}\.){2}[0-9a-f]{4}/i) {
70              push(@arpentries, { ip => $ip, mac => $mac });
71        }
72    }
73
74
75    $expect->send("exit\n");
76    $expect->hard_close();
77
78    return @arpentries;
79}
80
811;
82