From 2f7e3f80a3e9c199db3b8771207c8ba23db37452 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 13 Jul 2023 13:33:24 +0100 Subject: [PATCH] #1066 try all communities on highest snmp version before lowering version and trying communities again --- lib/App/Netdisco/Transport/SNMP.pm | 31 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/App/Netdisco/Transport/SNMP.pm b/lib/App/Netdisco/Transport/SNMP.pm index ae14cdf7..209a0cce 100644 --- a/lib/App/Netdisco/Transport/SNMP.pm +++ b/lib/App/Netdisco/Transport/SNMP.pm @@ -210,22 +210,30 @@ sub _snmp_connect_generic { } my $info = undef; - COMMUNITY: foreach my $comm (@communities) { - next unless $comm; + my $orig_retries = $snmp_args{Retries}; + my $orig_timeout = $snmp_args{Timeout}; + + CLASS: foreach my $class (@classes) { + next unless $class; VERSION: foreach my $ver (@versions) { next unless $ver; + my %local_args = (%snmp_args, Version => $ver); - next if $ver eq 3 and exists $comm->{community}; - next if $ver ne 3 and !exists $comm->{community}; + COMMUNITY: foreach my $comm (@communities) { + next unless $comm; - CLASS: foreach my $class (@classes) { - next unless $class; + next if $ver eq 3 and exists $comm->{community}; + next if $ver ne 3 and !exists $comm->{community}; + + # $local_args{Retries} = $comm->{_tried} ? $orig_retries : 0; + # $local_args{Timeout} = $comm->{_tried} ? $orig_timeout : 500000; - my %local_args = (%snmp_args, Version => $ver); $info = _try_connect($device, $class, $comm, $mode, \%local_args, ($useclass ? 0 : 1) ); - last COMMUNITY if $info; + last CLASS if $info; + + # ++$comm->{_tried}; } } } @@ -247,9 +255,10 @@ sub _try_connect { try { debug - sprintf '[%s:%s] try_connect with ver: %s, class: %s, comm: %s', + sprintf '[%s:%s] try_connect with v: %s, t: %s, r: %s, class: %s, comm: %s', $snmp_args->{DestHost}, $snmp_args->{RemotePort}, - $snmp_args->{Version}, $class, $debug_comm; + $snmp_args->{Version}, ($snmp_args->{Timeout} / 1000000), $snmp_args->{Retries}, + $class, $debug_comm; Module::Load::load $class; $info = $class->new(%$snmp_args, %comm_args) or return; @@ -260,7 +269,7 @@ sub _try_connect { if ($reclass and $info and $info->device_type ne $class) { $class = $info->device_type; debug - sprintf '[%s:%s] try_connect with ver: %s, new class: %s, comm: %s', + sprintf '[%s:%s] try_connect with v: %s, new class: %s, comm: %s', $snmp_args->{DestHost}, $snmp_args->{RemotePort}, $snmp_args->{Version}, $class, $debug_comm;