Commit 282c5114 authored by Leigh B Stoller's avatar Leigh B Stoller

Allow search for a list of IPs on the command line. I still need to add

a web UI for this, since we often get a list of IPs from the security
desks.
parent 3a2a8b74
#!/usr/bin/perl -w #!/usr/bin/perl -w
# #
# Copyright (c) 2000-2018 University of Utah and the Flux Group. # Copyright (c) 2000-2019 University of Utah and the Flux Group.
# #
# {{{EMULAB-LICENSE # {{{EMULAB-LICENSE
# #
...@@ -56,8 +56,8 @@ my $TBOPS = "@TBOPSEMAIL@"; ...@@ -56,8 +56,8 @@ my $TBOPS = "@TBOPSEMAIL@";
# Protos # Protos
sub fatal($); sub fatal($);
sub SearchCurrent(); sub SearchCurrent($);
sub SearchManifest($); sub SearchManifest($$);
# #
# Turn off line buffering on output # Turn off line buffering on output
...@@ -130,13 +130,9 @@ if (defined($options{"t"})) { ...@@ -130,13 +130,9 @@ if (defined($options{"t"})) {
usage() usage()
if (!@ARGV); if (!@ARGV);
my $IP = shift(@ARGV); sub SearchCurrent($)
if ($IP !~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
fatal("Invalid IP format");
}
sub SearchCurrent()
{ {
my ($ip) = @_;
my $found; my $found;
# #
...@@ -158,7 +154,7 @@ sub SearchCurrent() ...@@ -158,7 +154,7 @@ sub SearchCurrent()
print STDERR "Could not parse manifest for $sliver\n"; print STDERR "Could not parse manifest for $sliver\n";
next; next;
} }
if (SearchManifest($manifest)) { if (SearchManifest($manifest, $ip)) {
$found = $sliver; $found = $sliver;
last; last;
} }
...@@ -186,8 +182,9 @@ sub SearchCurrent() ...@@ -186,8 +182,9 @@ sub SearchCurrent()
return $instance->uuid(); return $instance->uuid();
} }
sub SearchHistory() sub SearchHistory($)
{ {
my ($ip) = @_;
my @result = (); my @result = ();
my %dups = (); my %dups = ();
my $minmax = ""; my $minmax = "";
...@@ -204,7 +201,7 @@ sub SearchHistory() ...@@ -204,7 +201,7 @@ sub SearchHistory()
" apt_instance_history as h ". " apt_instance_history as h ".
"left join apt_instance_aggregate_history as ah on ". "left join apt_instance_aggregate_history as ah on ".
" ah.uuid=h.uuid ". " ah.uuid=h.uuid ".
"where $minmax ah.manifest like '%${IP}%'"); "where $minmax ah.manifest like '%${ip}%'");
while (my ($uuid,$aggregate_urn,$manifest) = while (my ($uuid,$aggregate_urn,$manifest) =
$query_result->fetchrow_array()) { $query_result->fetchrow_array()) {
...@@ -220,7 +217,7 @@ sub SearchHistory() ...@@ -220,7 +217,7 @@ sub SearchHistory()
print STDERR "Could not parse manifest for $uuid,$aggregate_urn\n"; print STDERR "Could not parse manifest for $uuid,$aggregate_urn\n";
next; next;
} }
if (SearchManifest($manifest)) { if (SearchManifest($manifest, $ip)) {
my $history_result = my $history_result =
DBQueryFatal("select pid,name from apt_instance_history ". DBQueryFatal("select pid,name from apt_instance_history ".
"where uuid='$uuid'"); "where uuid='$uuid'");
...@@ -246,9 +243,9 @@ sub SearchHistory() ...@@ -246,9 +243,9 @@ sub SearchHistory()
exit(0); exit(0);
} }
sub SearchManifest($) sub SearchManifest($$)
{ {
my ($manifest) = @_; my ($manifest, $ip) = @_;
# #
# Check the routable pool bindings first # Check the routable pool bindings first
...@@ -256,7 +253,7 @@ sub SearchManifest($) ...@@ -256,7 +253,7 @@ sub SearchManifest($)
my $pools = GeniXML::GetAddressPools($manifest); my $pools = GeniXML::GetAddressPools($manifest);
foreach my $pool (@{$pools}) { foreach my $pool (@{$pools}) {
foreach my $ipref (@{$pool->{'list'}}) { foreach my $ipref (@{$pool->{'list'}}) {
if ($ipref->{'ipv4'} eq $IP) { if ($ipref->{'ipv4'} eq $ip) {
return 1; return 1;
} }
} }
...@@ -274,37 +271,50 @@ sub SearchManifest($) ...@@ -274,37 +271,50 @@ sub SearchManifest($)
next next
if (!defined($ipv4)); if (!defined($ipv4));
if ($ipv4 eq $IP) { if ($ipv4 eq $ip) {
return 1; return 1;
} }
} }
return 0; return 0;
} }
if ($historical || $both) { if ($historical || $both) {
if ($both) { if (@ARGV == 1) {
my $uuid = SearchCurrent(); if ($both) {
my $uuid = SearchCurrent($ARGV[0]);
if (defined($webtask)) {
$webtask->instance($uuid);
}
}
my @list = SearchHistory($ARGV[0]);
if (defined($webtask)) { if (defined($webtask)) {
$webtask->instance($uuid); $webtask->instancelist(\@list);
$webtask->Exited(0);
} }
} }
my @list = SearchHistory(); else {
if (defined($webtask)) { fatal("Cannot search history for multiple IPs");
$webtask->instancelist(\@list);
$webtask->Exited(0);
} }
} }
else { else {
my $uuid = SearchCurrent(); if (@ARGV == 1) {
if (!defined($uuid)) { my $uuid = SearchCurrent($ARGV[0]);
if (!defined($uuid)) {
if (defined($webtask)) {
$webtask->output("No matching instance for IP");
$webtask->Exited(1);
}
exit(1);
}
if (defined($webtask)) { if (defined($webtask)) {
$webtask->output("No matching instance for IP"); $webtask->instance($uuid);
$webtask->Exited(1); $webtask->Exited(0);
} }
exit(1);
} }
if (defined($webtask)) { else {
$webtask->instance($uuid); # Command line only please.
$webtask->Exited(0); foreach my $ip (@ARGV) {
SearchCurrent($ip);
}
} }
} }
exit(0); exit(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment