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

📄 snmp.pm

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 PM
📖 第 1 页 / 共 5 页
字号:
   } 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+(?:\-*\w+)+))\.?(.*)$/);     my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/);     $varbind_list_ref = [[$tag, $iid]];   }   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]}; }sub STORE {    SNMP::_debug_internals($_[1]);    ${$_[0]} = $_[1];}sub DELETE {    SNMP::_debug_internals(0);    ${$_[0]} = undef;}package SNMP::DUMP_PACKET;# controls packet dump output from libsnmpsub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE { SNMP::_dump_packet($_[1]); ${$_[0]} = $_[1]; }sub DELETE { SNMP::_dump_packet(0); ${$_[0]} = 0; }package SNMP::MIB;sub TIEHASH {    bless {};}sub FETCH {    my $this = shift;    my $key = shift;    if (!defined $this->{$key}) {	tie(%{$this->{$key}}, SNMP::MIB::NODE, $key) or return undef;    }    $this->{$key};}sub STORE {    warn "STORE(@_) : write access to the MIB not implemented\n";}sub DELETE {    delete $_[0]->{$_[1]}; # just delete cache entry}sub FIRSTKEY { return '.1'; } # this should actually start at .0 but                              # because nodes are not stored in lexico                              # order in ucd-snmp node tree walk will                              # miss most of the treesub NEXTKEY { # this could be sped up by using an XS __get_next_oid maybe   my $node = $_[0]->FETCH($_[1])->{nextNode};   $node->{objectID};}sub EXISTS { exists $_[0]->{$_[1]} || $_[0]->FETCH($_[1]); }sub CLEAR { undef %{$_[0]}; } # clear the cachepackage SNMP::MIB::NODE;my %node_elements =    (     objectID => 0, # dotted decimal fully qualified OID     label => 0,    # leaf textual identifier (e.g., 'sysDescr')     subID => 0,    # leaf numeric OID component of objectID (e.g., '1')     moduleID => 0, # textual identifier for module (e.g., 'RFC1213-MIB')     parent => 0,   # parent node     children => 0, # array reference of children nodes     indexes => 0,  # returns array of column labels     varbinds => 0, # returns array of trap/notification varbinds     nextNode => 0, # next lexico node (BUG! does not return in lexico order)     type => 0,     # returns simple type (see getType for values)     access => 0,   # returns ACCESS (ReadOnly, ReadWrite, WriteOnly,                    # NoAccess, Notify, Create)     status => 0,   # returns STATUS (Mandatory, Optional, Obsolete,                    # Deprecated)     syntax => 0,   # returns 'textualConvention' if defined else 'type'     textualConvention => 0, # returns TEXTUAL-CONVENTION     units => 0,    # returns UNITS     hint => 0,     # returns HINT     enums => 0,    # returns hash ref {tag => num, ...}     ranges => 0,   # returns array ref of hash ref [{low => num, high => num}]     defaultValue => 0, # returns default value     description => 0, # returns DESCRIPTION ($SNMP::save_descriptions must                    # be set prior to MIB initialization/parsing     augments => 0, # textual identifier of augmented object    );# sub TIEHASH - implemented in SNMP.xs# sub FETCH - implemented in SNMP.xssub STORE {    warn "STORE(@_): write access to MIB node not implemented\n";}sub DELETE {    warn "DELETE(@_): write access to MIB node not implemented\n";}sub FIRSTKEY { my $k = keys %node_elements; (each(%node_elements))[0]; }sub NEXTKEY { (each(%node_elements))[0]; }sub EXISTS { exists($node_elements{$_[1]}); }sub CLEAR {    warn "CLEAR(@_): write access to MIB node not implemented\n";}#sub DESTROY {#    warn "DESTROY(@_): write access to MIB node not implemented\n";#    # print "SNMP::MIB::NODE::DESTROY : $_[0]->{label} ($_[0])\n";#}package SNMP::MIB::SAVE_DESCR;sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE { SNMP::_set_save_descriptions($_[1]); ${$_[0]} = $_[1]; }sub DELETE { SNMP::_set_save_descriptions(0); ${$_[0]} = 0; }package SNMP::MIB::REPLACE_NEWER;               # Controls MIB parsingsub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE {    SNMP::_set_replace_newer($_[1]);    ${$_[0]} = $_[1];}sub DELETE {    SNMP::_set_replace_newer(0);    ${$_[0]} = 0;}package SNMP::MIB::MIB_OPTIONS;sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE { SNMP::_mib_toggle_options($_[1]); ${$_[0]} = $_[1]; }sub DELETE { SNMP::_mib_toggle_options(0); ${$_[0]} = ''; }package SNMP;END{SNMP::_sock_cleanup() if defined &SNMP::_sock_cleanup;}# Autoload methods go after __END__, and are processed by the autosplit prog.1;__END__=head1 NAMESNMP - The Perl5 'SNMP' Extension Module for the Net-SNMP SNMP package.=head1 SYNOPSIS use SNMP; ... $sess = new SNMP::Session(DestHost => localhost, Community => public); $val = $sess->get('sysDescr.0'); ... $vars = new SNMP::VarList([sysDescr,0], [sysContact,0], [sysLocation,0]); @vals = $sess->get($vars); ... $vb = new SNMP::Varbind(); do {    $val = $sess->getnext($vb);    print "@{$vb}\n"; } until ($sess->{ErrorNum}); ... $SNMP::save_descriptions = 1; SNMP::initMib(); # assuming mib is not already loaded print "$SNMP::MIB{sysDescr}{description}\n";=head1 DESCRIPTION

⌨️ 快捷键说明

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