From 0ad577941a0152a978159fe8829377aeac1e1be3 Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Fri, 11 Dec 2015 14:50:47 +0900 Subject: [PATCH] handle NAT corner case for snmp-recon.pl --- .../util/recon_scripts/snmp-recon.pl | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pandora_server/util/recon_scripts/snmp-recon.pl b/pandora_server/util/recon_scripts/snmp-recon.pl index 071d65884a..b67288b1ea 100755 --- a/pandora_server/util/recon_scripts/snmp-recon.pl +++ b/pandora_server/util/recon_scripts/snmp-recon.pl @@ -762,8 +762,35 @@ sub host_connectivity($) { sub create_pandora_agent($) { my ($device) = @_; + my $agent; + my @agents = get_db_rows($DBH, + 'SELECT * FROM taddress, taddress_agent, tagente + WHERE tagente.id_agente = taddress_agent.id_agent + AND taddress_agent.id_a = taddress.id_a + AND ip = ?', $device + ); + # Does the host already exist? - my $agent = get_agent_from_addr($DBH, $device); + foreach my $candidate (@agents) { + $agent = {map {$_} %$candidate}; # copy contents, do not use shallow copy + # exclude $device itself, because it handle corner case when target includes NAT + my @registered = map {$_->{ip}} get_db_rows($DBH, + 'SELECT ip FROM taddress, taddress_agent, tagente + WHERE tagente.id_agente = taddress_agent.id_agent + AND taddress_agent.id_a = taddress.id_a + AND tagente.id_agente = ? + AND taddress.ip != ?', $agent->{id_agente}, $device + ); + foreach my $ip_addr (@registered) { + my @matched = grep { $_ =~ /^$ip_addr$/ } keys(%{$VISITED_DEVICES{$device}->{'addr'}}); + if (scalar(@matched) == 0) { + $agent = undef; + last; + } + } + last if(defined($agent)); # exit loop if match all ip_addr + } + if (!defined($agent)) { $agent = get_agent_from_name($DBH, $device); }