简体   繁体   中英

python snmp bad IP

I have problem pysnmp library. I`m trying to read info form my router SNMP. snmpwal works without problems...

from pysnmp.hlapi import *

router_ip = '192.168.88.254'
community = 'public'

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData(community),
           UdpTransportTarget((router_ip, 161)),
           ContextData(),

           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)),
           )
    )

if errorIndication:
    print(errorIndication)
elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
    for varBind in varBinds:
        device_name = varBind[1].prettyPrint()
        print(device_name)

and error is:

 python3 snmp.py
Traceback (most recent call last):
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 505, in resolveWithMib
    instIds = rowNode.getInstIdFromIndices(*self.__args[2:])
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/mibs/SNMPv2-SMI.py", line 1281, in getInstIdFromIndices
    syntax = mibObj.syntax.clone(indices[idx])
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/proto/rfc1902.py", line 232, in clone
    return univ.OctetString.clone(self, *args, **kwargs).setFixedLength(self.getFixedLength())
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pyasn1/type/base.py", line 376, in clone
    return self.__class__(value, **initializers)
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pyasn1/type/univ.py", line 837, in __init__
    base.SimpleAsn1Type.__init__(self, value, **kwargs)
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pyasn1/type/base.py", line 267, in __init__
    value = self.prettyIn(value)
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/proto/rfc1902.py", line 330, in prettyIn
    raise error.ProtocolError('Bad IP address syntax')
pysnmp.proto.error.ProtocolError: Bad IP address syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lamzaks/snmp.py", line 9, in <module>
    errorIndication, errorStatus, errorIndex, varBinds = next(
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 108, in getCmd
    cmdgen.getCmd(snmpEngine, authData, transportTarget,
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/hlapi/asyncore/cmdgen.py", line 130, in getCmd
    vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/hlapi/varbinds.py", line 39, in makeVarBinds
    __varBinds.append(varBind.resolveWithMib(mibViewController, ignoreErrors=False))
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 853, in resolveWithMib
    self.__args[0].resolveWithMib(mibViewController)
  File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 509, in resolveWithMib
    raise SmiError('Instance index %r to OID conversion failure at object %r: %s' % (
pysnmp.smi.error.SmiError: Instance index (0,) to OID conversion failure at object 'ipAdEntAddr': Bad IP address syntaxcaused by <class 'pysnmp.proto.error.ProtocolError'>: Bad IP address syntax

Does anyone had similar situation?

I`m trying to get info form SNMP devices.. if I use SNMPwalk -v2c -c public it works

Root cause

The reason behind is probably a bug in the SNMP agent you are testing,

https://github.com/lextudio/pysnmp/blob/v5.0.20/pysnmp/proto/rfc1902.py#L322

You can see from its source code that when it tries to parse incoming data from the SNMP agent, it simply expects all IP data to be IPv4 only. However, this agent seems to return unexpected bytes.

Tip to dig further

The unexpected bytes were not revealed in the error message, so if you want to dig deeper, you need to capture.network packets with a tool like Wireshark. Then you can easily analyze the bytes in Wireshark GUI.

Alternatively snmpwalk has a switch to dump the raw bytes. But you need to be an SNMP expert to read those bytes in raw format.

There are quite a few non-standard SNMP agents out there, so such issues are common. If this agent tries to return IPv6 addresses, it should use Ipv6Address textual convention, not IP data type, https://datatracker.ietf.org/doc/html/rfc2465

Why snmpwalk works?

Other popular SNMP libraries might be more tolerant in such cases. For example, snmpwalk command line utility is based on.NET-SNMP library written in C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM