?? snmp_util.pm
字號:
($binding, $bindings) = decode_sequence($bindings); ($oid, $value) = decode_by_template($binding, "%O%@"); $ok = 0; my $tempo = pretty_print($oid); foreach $noid (@vars) { if ($tempo =~ /^$noid\./) { $ok = 1; $upoid = $noid; last; } } if ($ok) { my $tmp = encode_oid_with_errmsg ($tempo); return undef unless defined $tmp; push @nnoid, $tmp; my $tempv = pretty_print($value); $tempo=~s/^$upoid\.//; push @retvals, "$tempo:$tempv"; } } last if ($#nnoid < 0); } if ($got) { return (@retvals); } else { $var = join(' ', @vars); warn "SNMPWALK Problem for $var on $host\n" unless ($SNMP_Session::suppress_warnings > 1); return undef; }}## A restricted snmpset.#sub snmpset(@) { my($host, @vars) = @_; my(@enoid, $response, $bindings, $binding); my($oid, @retvals, $type, $value); my $session; $session = &snmpopen($host); if (!defined($session)) { warn "SNMPSET Problem for $host\n" unless ($SNMP_Session::suppress_warnings > 1); return undef; } while(@vars) { ($oid) = toOID((shift @vars)); $type = shift @vars; $value = shift @vars; if ($type =~ /string/i) { $value = encode_string($value); push @enoid, [$oid,$value]; } elsif ($type =~ /ipaddr/i) { $value = encode_ip_address($value); push @enoid, [$oid,$value]; } elsif ($type =~ /int/i) { $value = encode_int($value); push @enoid, [$oid,$value]; } elsif ($type =~ /oid/i) { my $tmp = encode_oid_with_errmsg($value); return undef unless defined $tmp; push @enoid, [$oid,$tmp]; } else { warn "unknown SNMP type: $type\n" unless ($SNMP_Session::suppress_warnings > 1); return undef; } } if ($session->set_request_response(@enoid)) { $response = $session->pdu_buffer; ($bindings) = $session->decode_get_response($response); while ($bindings) { ($binding, $bindings) = decode_sequence($bindings); ($oid, $value) = decode_by_template($binding, "%O%@"); my $tempo = pretty_print($value); push @retvals, $tempo; } return (@retvals); } return undef;}## Send an SNMP trap#sub snmptrap(@) { my($host, $ent, $agent, $gen, $spec, @vars) = @_; my($oid, @retvals, $type, $value); my(@enoid); my $session; $host = $host . ':162' if !($host =~ /:/); $session = &snmpopen($host); if (!defined($session)) { warn "SNMPTRAP Problem for $host\n" unless ($SNMP_Session::suppress_warnings > 1); return undef; } if ($agent =~ /^\d+\.\d+\.\d+\.\d+(.*)/ ) { $agent = pack("C*", split /\./, $agent); } else { $agent = inet_aton($agent); } push @enoid, toOID(($ent)); push @enoid, encode_ip_address($agent); push @enoid, encode_int($gen); push @enoid, encode_int($spec); push @enoid, encode_timeticks((time-$agent_start_time) * 100); while(@vars) { ($oid) = toOID((shift @vars)); $type = shift @vars; $value = shift @vars; if ($type =~ /string/i) { $value = encode_string($value); push @enoid, [$oid,$value]; } elsif ($type =~ /int/i) { $value = encode_int($value); push @enoid, [$oid,$value]; } elsif ($type =~ /oid/i) { my $tmp = encode_oid_with_errmsg($value); return undef unless defined $tmp; push @enoid, [$oid,$tmp]; } else { warn "unknown SNMP type: $type\n" unless ($SNMP_Session::suppress_warnings > 1); return undef; } } return($session->trap_request_send(@enoid));}## Given an OID in either ASN.1 or mixed text/ASN.1 notation, return an# encoded OID.#sub toOID(@){ my(@vars) = @_; my($oid, $var, $tmp, $tmpv, @retvar); undef @retvar; foreach $var (@vars) { ($oid, $tmp) = &Check_OID($var); if (!$oid && $SNMP_util::CacheLoaded == 0) { $tmp = $SNMP_Session::suppress_warnings; $SNMP_Session::suppress_warnings = 1000; &snmpLoad_OID_Cache($SNMP_util::CacheFile); $SNMP_util::CacheLoaded = 1; $SNMP_Session::suppress_warnings = $tmp; ($oid, $tmp) = &Check_OID($var); } while (!$oid && $#SNMP_util::MIB_Files >= 0) { $tmp = $SNMP_Session::suppress_warnings; $SNMP_Session::suppress_warnings = 1000; snmpMIB_to_OID(shift(@SNMP_util::MIB_Files)); $SNMP_Session::suppress_warnings = $tmp; ($oid, $tmp) = &Check_OID($var); if ($oid) { open(CACHE, ">>$SNMP_util::CacheFile"); print CACHE "$tmp\t$oid\n"; close(CACHE); } } if ($oid) { $var =~ s/^$tmp/$oid/; } else { warn "Unknown SNMP var $var\n" unless ($SNMP_Session::suppress_warnings > 1); next; } print "toOID: $var\n" if $SNMP_util::Debug; $tmp = encode_oid_with_errmsg($var); return undef unless defined $tmp; push(@retvar, $tmp); } return @retvar;}## Add passed-in text, OID pairs to the OID mapping table.#sub snmpmapOID(@){ my(@vars) = @_; my($oid, $txt, $ind); $ind = 0; while($ind <= $#vars) { $txt = $vars[$ind++]; next unless($txt =~ /^(([a-z][a-z\d\-]*\.)*([a-z][a-z\d\-]*))$/i); $oid = $vars[$ind++]; next unless($oid =~ /^((\d+.)*\d+)$/); $SNMP_util::OIDS{$txt} = $oid; print "snmpmapOID: $txt => $oid\n" if $SNMP_util::Debug; } return undef;}## Open the passed-in file name and read it in to populate# the cache of text-to-OID map table. It expects lines# with two fields, the first the textual string like "ifInOctets",# and the second the OID value, like "1.3.6.1.2.1.2.2.1.10".## blank lines and anything after a '#' or between '--' is ignored.#sub snmpLoad_OID_Cache ($){ my($arg) = @_; my($txt, $oid); if (!open(CACHE, $arg)) { warn "snmpLoad_OID_Cache: Can't open $arg: $!" unless ($SNMP_Session::suppress_warnings > 1); return -1; } while(<CACHE>) { s/#.*//; s/--.*--//g; s/--.*//; next if (/^$/); next unless (/\s/); chop; ($txt, $oid) = split(' ', $_, 2); &snmpmapOID($txt, $oid); } close(CACHE); return 0;}## Check to see if an OID is in the text-to-OID cache.# Returns the OID and the corresponding text as two separate# elements.#sub Check_OID ($){ my($var) = @_; my($tmp, $tmpv, $oid); if ($var =~ /^(([a-z][a-z\d\-]*\.)*([a-z][a-z\d\-]*))/i) { $tmp = $&; $tmpv = $tmp; for (;;) { last if defined($SNMP_util::OIDS{$tmpv}); last if !($tmpv =~ s/^[^\.]*\.//); } $oid = $SNMP_util::OIDS{$tmpv}; if ($oid) { return ($oid, $tmp); } else { return undef; } } return ($var, $var);}## Save the passed-in list of MIB files until an OID can't be# found in the existing table. At that time the MIB file will# be loaded, and the lookup attempted again.#sub snmpQueue_MIB_File (@){ my(@files) = @_; my($file); foreach $file (@files) { push(@SNMP_util::MIB_Files, $file); }}## Read in the passed list of MIB files, parsing them# for their text-to-OID mappings#sub snmpMIB_to_OID ($){ my($arg) = @_; my($quote, $buf, $var, $code, $val, $tmp, $tmpv, $strt); my($ret); my(%Link) = ( 'org' => 'iso', 'dod' => 'org', 'internet' => 'dod', 'directory' => 'internet', 'mgmt' => 'internet', 'mib-2' => 'mgmt', 'experimental' => 'internet', 'private' => 'internet', 'enterprises' => 'private', ); if (!open(MIB, $arg)) { warn "snmpMIB_to_OID: Can't open $arg: $!" unless ($SNMP_Session::suppress_warnings > 1); return -1; } print "snmpMIB_to_OID: loading $arg\n" if $SNMP_util::Debug; $ret = 0; while(<MIB>) { s/--.*--//g; # throw away comments (-- anything --) s/--.*//; # throw away comments (-- anything EOL) if ($quote) { next unless /"/; $quote = 0; } chop; $buf = "$buf $_"; $buf =~ s/\s+/ /g; if ($buf =~ / DEFINITIONS ::= BEGIN/) { undef %Link; %Link = ( 'org' => 'iso', 'dod' => 'org', 'internet' => 'dod', 'directory' => 'internet', 'mgmt' => 'internet', 'mib-2' => 'mgmt', 'experimental' => 'internet', 'private' => 'internet', 'enterprises' => 'private', ); } $buf =~ s/OBJECT-TYPE/OBJECT IDENTIFIER/; $buf =~ s/MODULE-IDENTITY/OBJECT IDENTIFIER/; $buf =~ s/OBJECT-IDENTITY/OBJECT IDENTIFIER/; $buf =~ s/ IMPORTS .*\;//; $buf =~ s/ SEQUENCE {.*}//; $buf =~ s/ SYNTAX .*//; $buf =~ s/ [\w-]+ ::= OBJECT IDENTIFIER//; $buf =~ s/ OBJECT IDENTIFIER .* ::= {/ OBJECT IDENTIFIER ::= {/; $buf =~ s/".*"//; if ($buf =~ /"/) { $quote = 1; } if ($buf =~ / ([\w\-]+) OBJECT IDENTIFIER ::= {([^}]+)}/) { $var = $1; $buf = $2; undef $val; $buf =~ s/ +$//; ($code, $val) = split(' ', $buf, 2); if (length($val) <= 0) { $SNMP_util::OIDS{$var} = $code; $ret++; print "'$var' => '$code'\n" if $SNMP_util::Debug; } else { $strt = $code; while($val =~ / /) { ($tmp, $val) = split(' ', $val, 2); if ($tmp =~ /([\w\-]+)\((\d+)\)/) { $tmp = $1; $tmpv = "$SNMP_util::OIDS{$strt}.$2"; $Link{$tmp} = $strt; if (defined($SNMP_util::OIDS{$tmp})) { if ($tmpv ne $SNMP_util::OIDS{$tmp}) { $strt = "$strt.$tmp"; $SNMP_util::OIDS{$strt} = $tmpv; $ret++; } } else { $SNMP_util::OIDS{$tmp} = $tmpv; $ret++; $strt = $tmp; } } } if (!defined($SNMP_util::OIDS{$strt})) { warn "snmpMIB_to_OID: $arg: \"$strt\" prefix unknown, load the parent MIB first.\n" unless ($SNMP_Session::suppress_warnings > 1); } $Link{$var} = $strt; $val = "$SNMP_util::OIDS{$strt}.$val"; if (defined($SNMP_util::OIDS{$var})) { if ($val ne $SNMP_util::OIDS{$var}) { $var = "$strt.$var"; } } $SNMP_util::OIDS{$var} = $val; $ret++; print "'$var' => '$val'\n" if $SNMP_util::Debug; } undef $buf; } } close(MIB); return $ret;}sub encode_oid_with_errmsg ($) { my ($oid) = @_; my $tmp = encode_oid(split(/\./, $oid)); if (! defined $tmp) { warn "cannot encode Object ID $oid: $BER::errmsg" unless ($SNMP_Session::suppress_warnings > 1); return undef; } return $tmp;}1;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -