python - PySNMP 无法识别响应

标签 python snmp mib 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 } }
E..M..@.@.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 } }
E..b..@.@.I..]</.]<....'.N.\0D.....public.7....;.......0)0'..+..........).~.............@.........

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 } }
E..M..@.@.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 } }
E..b..@.@.I..]</.]<....'.N.\0D.....public.7....;.......0)0'..+..........).~.............@.........

如我们所见,设备返回响应 .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

你们能帮帮我吗?提前致谢,对不起我的英语。

最佳答案

您的 SNMP 代理似乎生成了损坏的 SNMP 消息。虽然 IPv4 地址的长度为四个八位字节,但您的代理会报告八个八位字节的值。

根据 SNMP RFC,pysnmp 会丢弃格式错误的 SNMP 消息并重试几次原始请求,以期获得正确的响应。

要使 pysnmp 使用特定格式错误的 IP 地址值,您可以在运行时修补其 IpAddress 类,使其仅从可能更长的初始化程序中获取四个前导八位字节:

>>> 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

关于python - PySNMP 无法识别响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242902/

相关文章:

ldap - LDAP 模式中 OID 号码的实验/私有(private)分支?

linux - 如何在 Linux 上使用 net-snmp 5.5 和 IPv6?

java - 为什么在 BER 编解码器中使用 Math.floor() 而不是整数除法

networking - 什么叫MIB-Module?

perl - 从 perl 中的 mib 值打印 mib 名称

Python 等同于格式字符串中的 C# 哈希 (#)

python - Pytorch CNN 损失没有变化,

python - 在 ffmpeg 中获取 rms 级别的减少数据

java - 如何使用 snmp4j-2.7.0 获取 snmpv3 中的系统信息

python - 有没有办法在两列pandas中找到重复数据删除?