2011-12-14 149 views
5

此打印是,我使用的用perl在net:snmp通過錶行走的代碼:MIB名從MIB值在Perl

 #! /usr/local/bin/perl 

     use strict; 
     use warnings; 

     use Net::SNMP qw(:snmp); 

     my $OID_hrSystem = '1.3.6.1.2.1.25.1'; 
     my $OID_ifPhysAddress = '1.3.6.1.2.1.2.2.1.6';   

     my ($session, $error) = Net::SNMP->session(
     -hostname => shift || 'localhost', 
     -community => shift || 'public', 
     -nonblocking => 1, 
     -translate => [-octetstring => 0], 
     -version  => 'snmpv2c', 
    ); 

     if (!defined $session) { 
     printf "ERROR: %s.\n", $error; 
     exit 1; 
     } 

     my %table; # Hash to store the results 

     my $result = $session->get_bulk_request(
     -varbindlist => [ $OID_hrSystem ], 
     -callback  => [ \&table_callback, \%table ], 
     -maxrepetitions => 10, 
    ); 

     if (!defined $result) { 
     printf "ERROR: %s\n", $session->error(); 
     $session->close(); 
     exit 1; 
     } 

     # Now initiate the SNMP message exchange. 

     snmp_dispatcher(); 

     $session->close(); 

     # Print the results, specifically formatting ifPhysAddress. 

     for my $oid (oid_lex_sort(keys %table)) { 
     if (!oid_base_match($OID_ifPhysAddress, $oid)) { 
      printf "%s = %s\n", $oid, $table{$oid};     
     } else {         
      printf "%s = %s\n", $oid, unpack 'H*', $table{$oid}; 
     }          
     } 

     exit 0; 

     sub table_callback 
     { 
     my ($session, $table) = @_; 

     my $list = $session->var_bind_list(); 

     if (!defined $list) { 
      printf "ERROR: %s\n", $session->error(); 
      return; 
     } 

     # Loop through each of the OIDs in the response and assign 
     # the key/value pairs to the reference that was passed with 
     # the callback. Make sure that we are still in the table 
     # before assigning the key/values. 

     my @names = $session->var_bind_names(); 
     my $next = undef; 

     while (@names) { 
      $next = shift @names; 
      if (!oid_base_match($OID_hrSystem, $next)) { 
       return; # Table is done. chakri 
      } 
      $table->{$next} = $list->{$next}; 
     } 

     # Table is not done, send another request, starting at the last 
     # OBJECT IDENTIFIER in the response. No need to include the 
     # calback argument, the same callback that was specified for the 
     # original request will be used. 

     my $result = $session->get_bulk_request(
      -varbindlist => [ $next ], 
      -maxrepetitions => 10, 
     ); 

     if (!defined $result) { 
      printf "ERROR: %s.\n", $session->error(); 
     } 

     return; 
     } 

輸出是:

1.3.6.1.2.1.25.1.1.0 = 1 hour, 12:00.77 

1.3.6.1.2.1.25.1.2.0 = � 
         + 

1.3.6.1.2.1.25.1.3.0 = 1536 

1.3.6.1.2.1.25.1.4.0 = BOOT_IMAGE=/boot/vmlinuz-3.0.0-14-generic root=UUID=5c4c8d22-3cea-4410-aaad-f297c75d217e ro quiet splash vt.handoff=7 

1.3.6.1.2.1.25.1.5.0 = 1 

1.3.6.1.2.1.25.1.6.0 = 133 

1.3.6.1.2.1.25.1.7.0 = 0 

但所需的輸出爲我如下:

hrSystemUptime.0 = 1:08:54.36 

hrSystemDate.0 = 2011-12-14,16:0:2.0,+1:0 

hrSystemInitialLoadDevice.0 = 1536 

hrSystemInitialLoadParameters.0 = "BOOT_IMAGE=/boot/vmlinuz-3.0.0-14-generic root=UUID=5c4c8d22-3cea-4410-aaad-f297c75d217e ro quiet splash vt.handoff=7" 

hrSystemNumUsers.0 = 1 

hrSystemProcesses.0 = 133 

hrSystemMaxProcesses.0 = 0 

輸出的主要事情是我想在T要打印MIB名他輸出而不是mib值

+1

是否[this SO question](http://stackoverflow.com/questions/2433187/is-there-a-simple-way-to-map-snmpmib-strings-to-oids-in-perl)適用於您? – CanSpice 2011-12-14 17:08:15

+0

是的,但它的作品,但我想我的方式結果 – 2012-01-28 15:14:37

回答

0

您是否在服務器上嘗試了snmpget命令?當我在CLI上直接運行snmpget時,結果帶有名稱:

例如:/ usr/local/bin/snmpget -OQ -v 2c -c社區xxxx .1.3.6.1.2.1.31.1.1.1.6.100663301 IF-MIB :: ifHCInOctets.100663301 = 152528664859348

如果它適用於您,您可能需要在PERL代碼中執行命令,而不是使用LIB。那麼你只需要處理輸出。

此外,土特產品可以使用snmptranslate到tranlate您的OID:

例如:在/ usr/local/bin目錄/ snmptranslate 1.3.6.1.2.1.25.1.1

HOST-RESOURCES-MIB :: hrSystemUptime

更多信息 - >http://www.net-snmp.org/wiki/index.php/TUT:snmptranslate

編輯

你爲什麼不:

my $pathSnmpTranslate = '/your/path/to/snmptranslate'; 

for my $oid (oid_lex_sort(keys %table)) { 
    my $oidTrans = `$pathSnmpTranslate $oid`; 
    if (!oid_base_match($OID_ifPhysAddress, $oid)) { 
     printf "%s = %s\n", $oidTrans, $table{$oid}; 
    } else {  
     printf "%s = %s\n", $oidTrans, unpack 'H*',$table{$oid}; 
    } 
} 

在我的機器,它的工作:

> /xxx % /usr/local/bin/snmptranslate 1.3.6.1.2.1.25.1.1
HOST-RESOURCES-MIB :: hrSystemUptime

> /xxx % /usr/local/bin/snmptranslate 1.3.6.1.2.1.25.1.1.0
HOST-RESOURCES-MIB :: hrSystemUptime.0

1

你可以使用SNMP module(在Ubuntu上可用,版本號爲libsnmp-perl),它爲加載的MIB提供了一個並列哈希, %SNMP::MIB。下面是一些示例代碼:

use SNMP; 
SNMP::initMib(); 
print "$SNMP::MIB{'1.3.6.1.2.1.25.1.1.0'}{label} = \n"; 
#Should print "hrSystemUptime = " 

因爲%SNMP::MIB是一個捆綁的哈希,你不能只是做一個檢查,並分配給一個詞法變量,即my $oid = $SNMP::MIB{$oidstr}。您必須每次都直接訪問它。

從MIB中加載了很多其他信息,包括數據類型,這可以幫助您解決與hrSystemDate相似的問題。另外,如果您需要加載特定的MIB,請參見the man page for mib_api。不過,您在示例中使用的那些默認情況下會在我的系統中加載。