⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp.pm

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 PM
📖 第 1 页 / 共 5 页
字号:
	if ($state->{'options'}{'callback'}) {	    if ($this->{Version} > 1 && !$state->{'options'}{'nogetbulk'}) {		$res = $this->getbulk(0, $state->{'repeatcount'}, $vbl,				      [\&_gettable_do_it, $this, $vbl,				       $parse_indexes, $textnode, $state]);	    } else {		$res = $this->getnext($vbl,				      [\&_gettable_do_it, $this, $vbl,				       $parse_indexes, $textnode, $state]);	    }	    return;	} else {	    if ($this->{Version} > 1 && !$state->{'options'}{'nogetbulk'}) {		$res = $this->getbulk(0, $state->{'repeatcount'}, $vbl);	    } else {		$res = $this->getnext($vbl);	    }	}    }    # finish up    _gettable_end_routine($state, $parse_indexes, $textnode);    # return the hash if no callback was specified    if (!$state->{'options'}{'callback'}) {	return($state->{'result_hash'});    }    #    # if they provided a callback, call it    #   (if an array pass the args as well)    #    if (ref($state->{'options'}{'callback'}) eq 'ARRAY') {	my $code = shift @{$state->{'options'}{'callback'}};	$code->(@{$state->{'options'}{'callback'}}, $state->{'result_hash'});    } else {	$state->{'options'}{'callback'}->($state->{'result_hash'});    }}sub _gettable_end_routine {    my ($state, $parse_indexes, $textnode) = @_;    if ($parse_indexes) {	my @indexes = @{$SNMP::MIB{$textnode}{'children'}[0]{'indexes'}};	my $i;	foreach my $trow (keys(%{$state->{'result_hash'}})) {	    my $noid = new NetSNMP::OID($state->{'columns'}[0] . "." . $trow);	    if (!$noid) {		print STDERR "***** ERROR parsing $state->{'columns'}[0].$trow MIB OID\n";		next;	    }	    my $nindexes = $noid->get_indexes();	    if (!$nindexes || ref($nindexes) ne 'ARRAY' ||		$#indexes != $#$nindexes) {		print STDERR "***** ERROR parsing $state->{'columns'}[0].$trow MIB indexes:\n  $noid => " . ref($nindexes) . "\n   [should be an ARRAY]\n  expended # indexes = $#indexes\n";		if (ref($nindexes) eq 'ARRAY') {		    print STDERR "***** ERROR parsing $state->{'columns'}[0].$trow MIB indexes: " . ref($nindexes) . " $#indexes $#$nindexes\n";		}		next;	    }	    for ($i = 0; $i <= $#indexes; $i++) {		$state->{'result_hash'}{$trow}{$indexes[$i]} = $nindexes->[$i];	    }	}    }}no strict;sub fget {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     $varbind_list_ref = [SNMP::split_vars($vars)];   }   my $cb = shift;   SNMP::_get($this, $this->{RetryNoSuch}, $varbind_list_ref, $cb);   foreach $varbind (@$varbind_list_ref) {     $sub = $this->{VarFormats}{$varbind->[$SNMP::Varbind::tag_f]} ||	 $this->{TypeFormats}{$varbind->[$SNMP::Varbind::type_f]};     &$sub($varbind) if defined $sub;     push(@res, $varbind->[$SNMP::Varbind::val_f]);   }   return(wantarray() ? @res : $res[0]);}sub getnext {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     $varbind_list_ref = [SNMP::split_vars($vars)];   }   my $cb = shift;   @res = SNMP::_getnext($this, $varbind_list_ref, $cb);   return(wantarray() ? @res : $res[0]);}sub fgetnext {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     $varbind_list_ref = [SNMP::split_vars($vars)];   }   my $cb = shift;   SNMP::_getnext($this, $varbind_list_ref, $cb);   foreach $varbind (@$varbind_list_ref) {     $sub = $this->{VarFormats}{$varbind->[$SNMP::Varbind::tag_f]} ||	 $this->{TypeFormats}{$varbind->[$SNMP::Varbind::type_f]};     &$sub($varbind) if defined $sub;     push(@res, $varbind->[$SNMP::Varbind::val_f]);   }   return(wantarray() ? @res : $res[0]);}sub getbulk {   my $this = shift;   my $nonrepeaters = shift;   my $maxrepetitions = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     $varbind_list_ref = [SNMP::split_vars($vars)];   }   my $cb = shift;   @res = SNMP::_getbulk($this, $nonrepeaters, $maxrepetitions, $varbind_list_ref, $cb);   return(wantarray() ? @res : $res[0]);}sub bulkwalk {   my $this = shift;   my $nonrepeaters = shift;   my $maxrepetitions = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {      $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {      $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {      $varbind_list_ref = [$vars];      $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {      # my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|\w+)\.?(.*)$/);      my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/);      $varbind_list_ref = [[$tag, $iid]];   }   if (scalar @$varbind_list_ref == 0) {      $this->{ErrorNum} = SNMP::constant("SNMPERR_GENERR", 0);      $this->{ErrorStr} = "cannot bulkwalk() empty variable list";      return undef;   }   if (scalar @$varbind_list_ref < $nonrepeaters) {      $this->{ErrorNum} = SNMP::constant("SNMPERR_GENERR", 0);      $this->{ErrorStr} = "bulkwalk() needs at least $nonrepeaters varbinds";      return undef;   }   my $cb = shift;   @res = SNMP::_bulkwalk($this, $nonrepeaters, $maxrepetitions,						$varbind_list_ref, $cb);   # Return, in list context, a copy of the array of arrays of Varbind refs.   # In scalar context, return either a reference to the array of arrays of   # Varbind refs, or the request ID for an asynchronous bulkwalk.  This is   # a compromise between the getbulk()-ish return, and the more useful array   # of arrays of Varbinds return from the synchronous bulkwalk().   #   return @res if (wantarray());   return defined($cb) ? $res[0] : \@res;}%trap_type = (coldStart => 0, warmStart => 1, linkDown => 2, linkUp => 3,	      authFailure => 4, egpNeighborLoss => 5, specific => 6 );sub trap {# (v1) enterprise, agent, generic, specific, uptime, <vars># $sess->trap(enterprise=>'.1.3.6.1.4.1.2021', # or 'ucdavis' [default]#             agent => '127.0.0.1', # or 'localhost',[default 1st intf on host]#             generic => specific,  # can be omitted if 'specific' supplied#             specific => 5,        # can be omitted if 'generic' supplied#             uptime => 1234,       # default to localhost uptime (0 on win32)#             [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars#                                                          # always last# (v2) oid, uptime, <vars># $sess->trap(uptime => 1234,#             oid => 'snmpRisingAlarm',#             [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars#                                                          # always last#                                                          # always last   my $this = shift;   my $vars = pop if ref($_[$#_]); # last arg may be varbind or varlist   my %param = @_;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   }   if ($this->{Version} eq '1') {       my $enterprise = $param{enterprise} || 'ucdavis';       $enterprise = SNMP::translateObj($enterprise)	   unless $enterprise =~ /^[\.\d]+$/;       my $agent = $param{agent} || '';       my $generic = $param{generic} || 'specific';       $generic = $trap_type{$generic} || $generic;       my $uptime = $param{uptime} || SNMP::_sys_uptime();       my $specific = $param{specific} || 0;       @res = SNMP::_trapV1($this, $enterprise, $agent, $generic, $specific,			  $uptime, $varbind_list_ref);   } elsif  (($this->{Version} eq '2')|| ($this->{Version} eq '2c')) {       my $trap_oid = $param{oid} || $param{trapoid} || '.0.0';       my $uptime = $param{uptime} || SNMP::_sys_uptime();       @res = SNMP::_trapV2($this, $uptime, $trap_oid, $varbind_list_ref);   }   return(wantarray() ? @res : $res[0]);}sub inform {# (v3) oid, uptime, <vars># $sess->inform(uptime => 1234,#             oid => 'coldStart',#             [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars#                                                          # then callback                                                           # always last   my $this = shift;   my $vars;   my $cb;   $cb = pop if ref($_[$#_]) eq 'CODE'; # last arg may be code   $vars = pop if ref($_[$#_]); # varbind or varlist   my %param = @_;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   }   my $trap_oid = $param{oid} || $param{trapoid};   my $uptime = $param{uptime} || SNMP::_sys_uptime();   if($this->{Version} eq '3') {     @res = SNMP::_inform($this, $uptime, $trap_oid, $varbind_list_ref, $cb);   } else {     warn("error:inform: This version doesn't support the command\n");   }   return(wantarray() ? @res : $res[0]);}package SNMP::TrapSession;@ISA = ('SNMP::Session');sub new {   my $type = shift;   # allow override of remote SNMP trap port   unless (grep(/RemotePort/, @_)) {       push(@_, 'RemotePort', 162); # push on new default for trap session   }   SNMP::Session::new($type, @_);}package SNMP::Varbind;$tag_f = 0;$iid_f = 1;$val_f = 2;$type_f = 3;$time_f = 4;sub new {   my $type = shift;   my $this = shift;   $this ||= [];   bless $this;}sub tag {  $_[0]->[$tag_f];}sub iid {  $_[0]->[$iid_f];}sub val {  $_[0]->[$val_f];}sub type {  $_[0]->[$type_f];}sub name {   if (defined($_[0]->[$iid_f]) && ($_[0]->[$iid_f] =~ m/^[0-9]+$/)) {      return $_[0]->[$tag_f] . "." . $_[0]->[$iid_f];   }   return $_[0]->[$tag_f];}sub fmt {    my $self = shift;    return $self->name . " = \"" . $self->val . "\" (" . $self->type . ")";}#sub DESTROY {#    print "SNMP::Varbind::DESTROY($_[0])\n";#}package SNMP::VarList;sub new {   my $type = shift;   my $this = [];   my $varb;   foreach $varb (@_) {     $varb = new SNMP::Varbind($varb) unless ref($varb) =~ /SNMP::Varbind/;     push(@{$this}, $varb);   }   bless $this;}#sub DESTROY {#    print "SNMP::VarList::DESTROY($_[0])\n";#}package SNMP::DEBUGGING;# controls info/debugging output from SNMP module and libsnmp# $SNMP::debugging == 1    =>   enables general info and warning output#                                (eqiv. to setting $SNMP::verbose)# $SNMP::debugging == 2    =>   enables do_debugging from libsnmp as well# $SNMP::debugging == 3    =>   enables packet_dump from libsnmp as wellsub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE {    $SNMP::verbose = $_[1];    SNMP::_set_debugging($_[1]>1);    $SNMP::dump_packet = ($_[1]>2);    ${$_[0]} = $_[1];}sub DELETE {    $SNMP::verbose = 0;    SNMP::_set_debugging(0);    $SNMP::dump_packet = 0;    ${$_[0]} = undef;}package SNMP::DEBUG_INTERNALS;		# Controls SNMP.xs debugging.sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -