#1119 accept filename to -d parameter on netdisco-do, strip whitespace from -d

This commit is contained in:
Oliver Gorwits
2023-11-09 22:55:01 +00:00
parent b5c0cd585e
commit 7ba080c0dc

View File

@@ -40,6 +40,7 @@ use Dancer qw/:moose :script/;
use Try::Tiny; use Try::Tiny;
use Pod::Usage; use Pod::Usage;
use Scalar::Util 'blessed'; use Scalar::Util 'blessed';
use File::Slurper 'read_lines';
use NetAddr::IP qw/:rfc3021 :lower/; use NetAddr::IP qw/:rfc3021 :lower/;
use App::Netdisco::Backend::Job; use App::Netdisco::Backend::Job;
@@ -102,14 +103,37 @@ unless ($action) {
use Moo; use Moo;
with 'App::Netdisco::Worker::Runner'; with 'App::Netdisco::Worker::Runner';
} }
my @hostlist = ();
foreach my $device (@$devices) { sub _test_device {
my $net = NetAddr::IP->new($device); my $d = shift or return;
if ($device and (!$net or $net->num == 0 or $net->addr eq '0.0.0.0')) { $d =~ s/\s//g;
info sprintf '%s: error - Bad host, IP or prefix: %s', $action, $device;
my $net = NetAddr::IP->new($d);
if ($d and (!$net or $net->num == 0 or $net->addr eq '0.0.0.0')) {
error sprintf 'unable to understand as host, IP, or prefix: %s', $d;
exit 1; exit 1;
} }
push(@hostlist,$net->hostenum) if defined $device;
return ($d ? $net->hostenum : undef);
}
my @hostlist = ();
foreach my $device (@$devices) {
if (-f $device) {
my @dlist = read_lines $device;
if (0 == scalar @dlist) {
error sprintf 'unable to read from file: %s', $device;
exit 2;
}
foreach my $entry (@dlist) {
push(@hostlist, _test_device($entry)) if _test_device($entry);
}
}
else {
push(@hostlist, _test_device($device)) if _test_device($device);
}
} }
my @job_specs = (); my @job_specs = ();
@@ -215,7 +239,8 @@ C<discover::neighbors>.
Any action taking a C<device> parameter can be passed either a hostname or IP Any action taking a C<device> parameter can be passed either a hostname or IP
address of any interface of a known or unknown device, or an IP prefix address of any interface of a known or unknown device, or an IP prefix
(subnet) which will cause C<netdisco-do> to run the action on all addresses in (subnet) which will cause C<netdisco-do> to run the action on all addresses in
that range. that range. The C<device> parameter may also be a filename that Netdisco will
open to read hostnames, IPs, or prefixes, one per line.
The C<device> parameter may be passed multiple times. In this case, all The C<device> parameter may be passed multiple times. In this case, all
addresses (after expanding IP Prefixes) will be handled one by one. addresses (after expanding IP Prefixes) will be handled one by one.