2012-04-20 230 views
5

PySNMP無法識別響應

from pysnmp.entity.rfc3413.oneliner import cmdgen 

errorIndication, errorStatus, errorIndex, \ 
varBindTable = cmdgen.CommandGenerator().bulkCmd(
      cmdgen.CommunityData('test-agent', 'public'), 
      cmdgen.UdpTransportTarget(('IP.IP.IP.IP', 161)), 
      0, 
      1, 
      (1,3,6,1,2,1,4,24,4,1,2,169,254) 
     ) 

if errorIndication: 
    print errorIndication 
else: 
    if errorStatus: 
     print '%s at %s\n' % (
      errorStatus.prettyPrint(), 
      errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' 
      ) 
    else: 
     for varBindTableRow in varBindTable: 
      for name, val in varBindTableRow: 
       print '%s = %s' % (name.prettyPrint(), val.prettyPrint()) 

使用命令行運行snmpwalk到該設備的回報預期的結果。但是 腳本返回在超時之前沒有收到SNMP響應。如果我省略這個OID,那麼一切正常。 所以,問題是在這個OID

這裏tcpdump的統計:

/usr/sbin/tcpdump -nn -vv -s0 -A host HOST and udp 

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 

12:15:31.494920 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto: UDP (17), length: 77) IP.IP.IP.IP.47911 > IP.IP.IP.IP.161: [bad udp cksum 4b7d!] { SNMPv2c { GetBulk(34) R=8993731 N=0 M=1 .1.3.6.1.2.1.4.24.4.1.2.169.254 } } 
[email protected]@.I..]<..]</.'...9.S0/.....public."....;.......0.0...+..........).~.. 

12:15:31.495666 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto: UDP (17), length: 98) IP.IP.IP.IP.161 > IP.IP.IP.IP.47911: [udp sum ok] { SNMPv2c { GetResponse(55) R=8993731 .1.3.6.1.2.1.4.24.4.1.2.169.254.0.0.0.0.255.255.0.0.0.0.0=[inetaddr len!=4]0.0.255.255.0.0.0.0 } } 
[email protected]@.I..]</.]<....'.N.\0D.....public.7....;.......0)0'..+..........)[email protected] 

12:15:32.500226 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto: UDP (17), length: 77) IP.IP.IP.IP.47911 > IP.IP.IP.IP.161: [bad udp cksum 4b7d!] { SNMPv2c { GetBulk(34) R=8993731 N=0 M=1 .1.3.6.1.2.1.4.24.4.1.2.169.254 } } 
[email protected]@.I..]<..]</.'...9.S0/.....public."....;.......0.0...+..........).~.. 

12:15:32.500624 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto: UDP (17), length: 98) IP.IP.IP.IP.161 > IP.IP.IP.IP.47911: [udp sum ok] { SNMPv2c { GetResponse(55) R=8993731 .1.3.6.1.2.1.4.24.4.1.2.169.254.0.0.0.0.255.255.0.0.0.0.0=[inetaddr len!=4]0.0.255.255.0.0.0.0 } } 
[email protected]@.I..]</.]<....'.N.\0D.....public.7....;.......0)0'..+..........)[email protected] 

我們可以看到,設備返回響應.1.3.6.1.2.1.4.24.4.1.2.169.254.0.0.0.0.255.255.0.0.0.0.0=[inetaddr len!=4]0.0.255.255.0.0.0.0,但沒有任何反應和pysnmp只是不斷地一次又一次地嘗試這個OID的值.. snmpwalk認識到這個反應爲IP ADDRESS 0.0.255.255

你們能幫我嗎?預先感謝並抱歉我的英語。

回答

4

您的SNMP代理似乎產生損壞的SNMP消息。雖然IPv4地址長度爲四個八位字節,但您的代理報告八個八位字節的值。

根據SNMP RFC,pysnmp會丟棄格式錯誤的SNMP消息並重試原始請求幾次,以期獲得正確的響應。

爲了pysnmp與具體格式不正確IP地址值的工作,你可以在運行時修補的Ip地址類,使其從一個可能更長初始化他僅用四大主導字節:

>>> def ipAddressPrettyIn(self, value): 
... return origIpAddressPrettyIn(self, value[:4]) 
... 
>>> origIpAddressPrettyIn = v2c.IpAddress.prettyIn 
>>> v2c.IpAddress.prettyIn = ipAddressPrettyIn 
>>> 
>>> msg, rest = decoder.decode(wholeMsg, asn1Spec=v2c.Message()) 
>>> print msg.prettyPrint() 
Message: 
version='version-2' 
community=public 
data=PDUs: 
response=ResponsePDU: 
    request-id=6564368 
    error-status='noError' 
    error-index=0 
    variable-bindings=VarBindList: 
    VarBind: 
    name=1.3.6.1.2.1.4.24.4.1.2.169.254.0.0.0.0.255.255.0.0.0.0.0 
    =_BindValue: 
    value=ObjectSyntax: 
     application-wide=ApplicationSyntax: 
     ipAddress-value=0.0.255.255 
+0

再次謝謝:) – 2012-04-23 06:32:24