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.
This commit is contained in:
Bill Fenner
2007-12-02 18:55:54 +00:00
parent a9481c86d2
commit 9a0915f3a1

54
Info.pm
View File

@@ -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()