From 9a0915f3a15999a5edd11ea00b7426a1cbf63bb4 Mon Sep 17 00:00:00 2001 From: Bill Fenner <> Date: Sun, 2 Dec 2007 18:55:54 +0000 Subject: [PATCH] Infrastructure for SNMPv3 support: - Save the SecName passed into the constructor - Create an update() function, which replaces the underlying SNMP session using different parameters. - Return the SecName instead of community from snmp_comm() when using SNMPv3. This is a little wrong, but is probably better than returning undef. --- Info.pm | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/Info.pm b/Info.pm index d630ad56..1c79332c 100644 --- a/Info.pm +++ b/Info.pm @@ -905,11 +905,59 @@ sub new { $new_obj->{args} = \%args; $new_obj->{snmp_ver} = $args{Version} || 2; $new_obj->{snmp_comm} = $args{Community} || 'public'; + $new_obj->{snmp_user} = $args{SecName} || 'initial'; return $auto_specific ? $new_obj->specify() : $new_obj; } +=item update() + +Replace the existing session with a new one with updated values, +without re-identifying the device. The only supported changes are +to Community or Context. + +Clears the object cache. + +This is useful, e.g., when a device supports multiple contexts +(via changes to the Community string, or via the SNMPv3 Context +parameter), but a context that you want to access does not support +the objects (e.g., sysObjectID, sysDescr) that we use to identify +the device. + +=cut + +sub update { + my $obj = shift; + my %update_args = @_; + my %sess_args = (%{$obj->{args}}, %update_args); + + # silently only update "the right" args + delete $sess_args{Debug}; + delete $sess_args{DebugSNMP}; + delete $sess_args{AutoSpecify}; + delete $sess_args{BulkRepeaters}; + delete $sess_args{BulkWalk}; + delete $sess_args{LoopDetect}; + delete $sess_args{BigInt}; + delete $sess_args{MibDirs}; + + my $sess = new SNMP::Session( 'UseEnums' => 1, %sess_args , 'RetryNoSuch' => $obj->{nosuch}); + unless (defined $sess){ + $obj->error_throw("SNMP::Info::update() Failed to Create new Session. "); + return undef; + } + + # Session object created but SNMP connection failed. + my $sess_err = $sess->{ErrorStr} || ''; + if ($sess_err){ + $obj->error_throw("SNMP::Info::update() Net-SNMP session creation failed. $sess_err"); + return undef; + } + $obj->clear_cache(); + return $obj->session($sess); +} + =back =head2 Data is Cached @@ -1288,7 +1336,11 @@ Returns SNMP Community string used in connection. sub snmp_comm { my $self = shift; - return $self->{snmp_comm}; + if ($self->{snmp_ver} == 3) { + return $self->{snmp_user}; + } else { + return $self->{snmp_comm}; + } } =item $info->snmp_ver()