📄 snmp.pm
字号:
NetSNMP::default_store::NETSNMP_DS_LIB_SECNAME()) || 'initial'; if (!$this->{SecLevel}) { $this->{SecLevel} = NetSNMP::default_store::netsnmp_ds_get_int(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(), NetSNMP::default_store::NETSNMP_DS_LIB_SECLEVEL()) || $SNMP::V3_SEC_LEVEL_MAP{'noAuthNoPriv'}; } elsif ($this->{SecLevel} !~ /^\d+$/) { $this->{SecLevel} = $SNMP::V3_SEC_LEVEL_MAP{$this->{SecLevel}}; } $this->{SecEngineId} ||= ''; $this->{ContextEngineId} ||= $this->{SecEngineId}; $this->{Context} ||= NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(), NetSNMP::default_store::NETSNMP_DS_LIB_CONTEXT()) || ''; $this->{AuthProto} ||= 'DEFAULT'; # defaults to the library's default $this->{AuthPass} ||= NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(), NetSNMP::default_store::NETSNMP_DS_LIB_AUTHPASSPHRASE()) || NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(), NetSNMP::default_store::NETSNMP_DS_LIB_PASSPHRASE()) || ''; $this->{AuthMasterKey} ||= ''; $this->{PrivMasterKey} ||= ''; $this->{AuthLocalizedKey} ||= ''; $this->{PrivLocalizedKey} ||= ''; $this->{PrivProto} ||= 'DEFAULT'; # defaults to hte library's default $this->{PrivPass} ||= NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(), NetSNMP::default_store::NETSNMP_DS_LIB_PRIVPASSPHRASE()) || NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(), NetSNMP::default_store::NETSNMP_DS_LIB_PASSPHRASE()) || ''; $this->{EngineBoots} = 0 if not defined $this->{EngineBoots}; $this->{EngineTime} = 0 if not defined $this->{EngineTime}; $this->{SessPtr} = SNMP::_new_v3_session($this->{Version}, $this->{DestHost}, $this->{Retries}, $this->{Timeout}, $this->{SecName}, $this->{SecLevel}, $this->{SecEngineId}, $this->{ContextEngineId}, $this->{Context}, $this->{AuthProto}, $this->{AuthPass}, $this->{PrivProto}, $this->{PrivPass}, $this->{EngineBoots}, $this->{EngineTime}, $this->{AuthMasterKey}, length($this->{AuthMasterKey}), $this->{PrivMasterKey}, length($this->{PrivMasterKey}), $this->{AuthLocalizedKey}, length($this->{AuthLocalizedKey}), $this->{PrivLocalizedKey}, length($this->{PrivLocalizedKey}), ); } unless ($this->{SessPtr}) { warn("unable to create session") if $SNMP::verbose; return undef; } SNMP::initMib($SNMP::auto_init_mib); # ensures that *some* mib is loaded $this->{UseLongNames} ||= $SNMP::use_long_names; $this->{UseSprintValue} ||= $SNMP::use_sprint_value; $this->{BestGuess} ||= $SNMP::best_guess; $this->{UseEnums} ||= $SNMP::use_enums; $this->{UseNumeric} ||= $SNMP::use_numeric; # Force UseLongNames if UseNumeric is in use. $this->{UseLongNames}++ if $this->{UseNumeric}; bless $this, $type;}sub update {# *Not Implemented*# designed to update the fields of session to allow retargetting to different# host, community name change, timeout, retry changes etc. Unfortunately not# working yet because some updates (the address in particular) need to be# done on the internal session pointer which cannot be fetched w/o touching# globals at this point which breaks win32. A patch to the ucd-snmp toolkit# is needed my $this = shift; my ($name, $aliases, $host_type, $len, $thisaddr); my %new_fields = @_; @$this{keys %new_fields} = values %new_fields; $this->{UseLongNames} ||= $SNMP::use_long_names; $this->{UseSprintValue} ||= $SNMP::use_sprint_value; $this->{BestGuess} ||= $SNMP::best_guess; $this->{UseEnums} ||= $SNMP::use_enums; $this->{UseNumeric} ||= $SNMP::use_numeric; # Force UseLongNames if UseNumeric is in use. $this->{UseLongNames}++ if $this->{UseNumeric}; SNMP::_update_session($this->{Version}, $this->{Community}, $this->{DestHost}, $this->{RemotePort}, $this->{LocalPort}, $this->{Retries}, $this->{Timeout}, );}sub set { my $this = shift; my $vars = shift; my $varbind_list_ref; my $res = 0; 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+(?:\-*\w+)+))\.?(.*)$/); my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/); my $val = shift; $varbind_list_ref = [[$tag, $iid, $val]]; } my $cb = shift; $res = SNMP::_set($this, $varbind_list_ref, $cb);}sub get { 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 { # my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/); my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/); $varbind_list_ref = [[$tag, $iid]]; } my $cb = shift; @res = SNMP::_get($this, $this->{RetryNoSuch}, $varbind_list_ref, $cb); return(wantarray() ? @res : $res[0]);}$have_netsnmp_oid = eval { require NetSNMP::OID; };sub gettable { # # getTable # -------- # # Get OIDs starting at $table_oid, and continue down the tree # until we get to an OID which does not start with $table_oid, # i.e. we have reached the end of this table. # my ($this, $root_oid, $options) = @_; my ($textnode, $stopconds, $varbinds, $vbl, $res, %result_hash, $repeat); # translate the OID into numeric form if its not if ($root_oid !~ /^[\.0-9]+$/) { $textnode = $root_oid; $root_oid = SNMP::translateObj($root_oid); } else { $textnode = SNMP::translateObj($root_oid); } # bail if we don't have a valid oid. return if (!$root_oid); # deficed if we're going to parse indexes my $parse_indexes = (defined($options->{'noindexes'})) ? 0 : $have_netsnmp_oid; # get the list of columns we should look at. my @columns; if (!$options->{'columns'}) { if ($textnode) { my %indexes; if ($parse_indexes) { # get indexes my @indexes = @{$SNMP::MIB{$textnode}{'children'}[0]{'indexes'}}; # quick translate into a hash map { $indexes{$_} = 1; } @indexes; } # calculate the list of accessible columns that aren't indexes my $children = $SNMP::MIB{$textnode}{'children'}[0]{'children'}; foreach my $c (@$children) { push @columns, $root_oid . ".1." . $c->{'subID'} if (!$indexes{$c->{'label'}}); } if ($#columns == -1) { # some tables are only indexes, and we need to walk at # least one column. We pick the last. push @columns, $root_oid . ".1." . $children->[$#$children]{'subID'}; } } } else { # XXX: requires specification in numeric OID... ack.! @columns = @{$options->{'columns'}}; } # create the initial walking info. foreach my $c (@columns) { push @$varbinds, [$c]; push @$stopconds, $c; } $vbl = $varbinds; my $repeatcount; if ($opts->{nogetbulk}) { $repeatcount = 1; } elsif ($options->{'repeat'}) { $repeatcount = $options->{'repeat'}; } else { # experimentally determined maybe guess at a best repeat value # 1000 bytes max (safe), 30 bytes average for encoding of the # varbind (experimentally determined to be closer to # 26. Again, being safe. Then devide by the number of # varbinds. $repeatcount = int(1000 / 36 / ($#$varbinds + 1)); } if ($this->{Version} > 1 && !$options->{'nogetbulk'}) { $res = $this->getbulk(0, $repeatcount, $vbl); } else { $res = $this->getnext($vbl); } while ($#$vbl > -1 && !$this->{ErrorNum}) { if ($#$vbl + 1 != ($#$stopconds + 1) * $repeatcount) { print STDERR "ack: gettable results not appropriate\n"; my @k = keys(%result_hash); last if ($#k > -1); # bail with what we have return; } $varbinds = []; my $newstopconds; my $lastsetstart = ($repeatcount-1) * ($#$stopconds+1); for (my $i = 0; $i <= $#$vbl; $i++) { my $row_oid = SNMP::translateObj($vbl->[$i][0]); my $row_text = $vbl->[$i][0]; my $row_index = $vbl->[$i][1]; my $row_value = $vbl->[$i][2]; my $row_type = $vbl->[$i][3]; if ($row_oid =~ /^$stopconds->[$i % ($#$stopconds+1)]/) { if ($row_type eq "OBJECTID") { # If the value returned is an OID, translate this # back in to a textual OID $row_value = SNMP::translateObj($row_value); } # Place the results in a hash $result_hash{$row_index}{$row_text} = $row_value; # continue past this next time if ($i >= $lastsetstart) { push @$newstopconds, $stopconds->[$i%($#$stopconds+1)]; push @$varbinds,[$vbl->[$i][0],$vbl->[$i][1]]; } } } if ($#$newstopconds == -1) { last; } if ($#$varbinds == -1) { print "gettable ack. shouldn't get here\n"; } $vbl = $varbinds; $stopconds = $newstopconds; if ($this->{Version} > 1 && !$options->{'nogetbulk'}) { $res = $this->getbulk(0, $repeatcount, $vbl); } else { $res = $this->getnext($vbl); } } # calculate indexes if ($parse_indexes) { my @indexes = @{$SNMP::MIB{$textnode}{'children'}[0]{'indexes'}}; my $i; foreach my $trow (keys(%result_hash)) { my $noid = new NetSNMP::OID($columns[0] . "." . $trow); if (!$noid) { print STDERR "***** ERROR parsing $columns[0].$trow MIB OID\n"; next; } my $nindexes = $noid->get_indexes(); if (!$nindexes || ref($nindexes) ne 'ARRAY' || $#indexes != $#$nindexes) { print STDERR "***** ERROR parsing $columns[0].$trow MIB indexes: $noid => " . ref($nindexes) . " [should be an ARRAY], expended # indexes = $#indexes\n"; if (ref($nindexes) eq 'ARRAY') { print STDERR "***** ERROR parsing $columns[0].$trow MIB indexes: " . ref($nindexes) . " $#indexes $#$nindexes\n"; } next; } for ($i = 0; $i <= $#indexes; $i++) { $result_hash{$trow}{$indexes[$i]} = $nindexes->[$i]; } } } return(\%result_hash);}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 { # my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/); my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/); $varbind_list_ref = [[$tag, $iid]]; } 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 { # my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/); my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/); $varbind_list_ref = [[$tag, $iid]]; } 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 { # my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/); my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/); $varbind_list_ref = [[$tag, $iid]]; } 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -