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

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 156 CC 12 SCAR 31.1
CC
12
Cov
0%
arpnip
42.0
get_paginated_output
30.0
arpnip_context
12.0
CRAP 22.5
1 package App::Netdisco::SSHCollector::Platform::FortiOS;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::FortiOS
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP entries from Fortinet FortiOS Fortigate devices.
10  
11 =cut
12  
13 use strict;
14 use warnings;
15  
16 use Dancer ':script';
17 use Moo;
18 use Expect;
19 use Regexp::Common qw(net);
20  
21 =head1 PUBLIC METHODS
22  
23 =over 4
24  
25 =item B<arpnip($host, $ssh)>
26  
27 Retrieve ARP entries from device. C<$host> is the hostname or IP address
28 of the device. C<$ssh> is a Net::OpenSSH connection to the device.
29  
30 If a post-login banner needs to be accepted, please set C<$banner> to true.
31  
32 Returns a list of hashrefs in the format C<< { mac => MACADDR, ip => IPADDR } >>.
33  
34 =back
35  
36 =cut
37  
38 my $prompt = qr/ [\$#] +$/;
39 my $more_pattern = qr/--More--/;
40 my $timeout = 10;
41  
42 sub get_paginated_output {
43 my ($command, $expect) = @_;
44 my $more_flag = 0;
45 my @lines = undef;
46 my @alllines = undef;
47 $expect->send($command."\n");
48 while (1) {
49 my ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt, -re, $more_pattern);
50 if ($match) {
51 if ($match =~ $more_pattern) {
52 $more_flag = 1;
53 @lines = split(/\R/, $before);
54 push(@alllines, grep {$_ =~ /\S/} @lines);
55 debug("skipping through --More-- pagination");
56 $expect->send(" ");
57 } elsif ($match =~ $prompt) {
58 $more_flag = 0;
59 @lines = split(/\R/, $before);
60 push(@alllines, grep {$_ =~ /\S/} @lines);
61 foreach my $line (@alllines) {
62 debug("output collected: $line") if $line;
63 }
64 last;
65 }
66 }
67 }
68  
69 return @alllines;
70 }
71  
72 sub arpnip_context {
73 my ($expect, $prompt, $timeout, $arpentries) = @_;
74  
75 # IPv4 ARP
76 ##########
77  
78 my @data = get_paginated_output("get system arp", $expect);
79  
80 # fortigate # get system arp
81 # Address Age(min) Hardware Addr Interface
82 # 2.6.0.5 0 00:40:46:f9:63:0f PLAY-0400
83 # 1.2.9.7 2 00:30:59:bc:f6:94 DEAD-3550
84  
85 my $re_ipv4_arp = qr/^($RE{net}{IPv4})\s*\d+\s*($RE{net}{MAC})\s*\S+$/;
86 foreach (@data) {
87 #if (/^($RE{net}{IPv4})\s*\d+\s*($RE{net}{MAC})\s*\S+$/) {
88 if ($_ && /$re_ipv4_arp/) {
89 debug "\tfound IPv4: $1 => MAC: $2";
90 push(@$arpentries, { ip => $1, mac => $2 });
91 }
92 }
93  
94 # IPv6 ND
95 ##########
96  
97 @data = get_paginated_output("diagnose ipv6 neighbor-cache list", $expect);
98  
99 # fortigate # diagnose ipv6 neighbor-cache list
100 # ifindex=403 ifname=WORK-4016 fe80::abcd:1234:dead:f00d ab:cd:ef:01:23:45 state=00000004 use=42733 confirm=42733 update=41100 ref=3
101 # ifindex=67 ifname=PLAY-4036 ff02::16 33:33:00:00:00:16 state=00000040 use=4765 confirm=10765 update=4765 ref=0
102 # ifindex=28 ifname=root :: 00:00:00:00:00:00 state=00000040 use=589688110 confirm=589694110 update=589688110 ref=1
103 # ifindex=48 ifname=FUN-4024 2001:42:1234:fe80:1234:1234:1234:1234 b0:c1:e2:f3:a4:b5 state=00000008 use=12 confirm=2 update=12 ref=2
104  
105 # might fail with: Unknown action 0 - this is a permission issue of the logged in user
106  
107 my $re_ipv6_arp = qr/^ifindex=\d+\s+ifname=\S+\s+($RE{net}{IPv6}{-sep => ':'}{-style => 'HeX'})\s+($RE{net}{MAC}).*$/;
108 foreach (@data) {
109 if ($_ && /$re_ipv6_arp/) {
110 debug "\tfound IPv6: $1 => MAC: $2";
111 push(@$arpentries, { ip => $1, mac => $2 });
112 }
113 }
114 }
115  
116 sub arpnip {
117 my ($self, $hostlabel, $ssh, $args) = @_;
118  
119 debug "$hostlabel $$ arpnip()";
120  
121 my ($pty, $pid) = $ssh->open2pty;
122 unless ($pty) {
123 warn "unable to run remote command [$hostlabel] " . $ssh->error;
124 return ();
125 }
126  
127 $Expect::Debug = 0;
128 $Expect::Exp_Internal = 0;
129  
130 my $expect = Expect->init($pty);
131 $expect->raw_pty(1);
132  
133 my ($pos, $error, $match, $before, $after);
134  
135 if ($args->{banner}) {
136 my $banner = qr/^\(Press 'a' to accept\):/;
137 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $banner);
138  
139 $expect->send("a");
140 }
141 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
142  
143 my @data = get_paginated_output("get system status", $expect);
144 my $multi_vdom = 0;
145 foreach (@data) {
146 if ($_ && /^Virtual domain configuration: (multiple|split-task)$/) {
147 $multi_vdom = 1;
148 last;
149 }
150 }
151 my $arpentries = [];
152 if ($multi_vdom) {
153 $expect->send("config global\n");
154 $expect->expect($timeout, -re, $prompt);
155  
156 my @data = get_paginated_output("get system vdom-property", $expect);
157 my $vdoms = [];
158 foreach (@data) {
159 push(@$vdoms, $1) if $_ && (/^==\s*\[\s*(\S+)\s*\]$/);
160 }
161  
162 $expect->send("end\n");
163 $expect->expect($timeout, -re, $prompt);
164  
165 foreach (@$vdoms) {
166 $expect->send("config vdom\n");
167 $expect->expect($timeout, -re, $prompt);
168 $expect->send("edit $_\n");
169 debug ("switched to config vdom; edit $_");
170 $expect->expect($timeout, -re, $prompt);
171 arpnip_context($expect, $prompt, $timeout, $arpentries);
172 $expect->send("end\n");
173 $expect->expect($timeout, -re, $prompt);
174 }
175 } else {
176 arpnip_context($expect, $prompt, $timeout, $arpentries);
177 }
178 $expect->send("exit\n");
179 $expect->soft_close();
180  
181 return @$arpentries;
182 }
183  
184 1;