etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
582 stars 200 forks source link

contextName failing. V3 to cisco switch #284

Closed gittyhubbyfrankybobby closed 5 years ago

gittyhubbyfrankybobby commented 5 years ago

this snmpwalk command succeeds using context "vlan-10" snmpwalk -v3 -l authPriv -u username -a SHA -A asdfasdfasdf -x DES -X asdfasdfasdf -n vlan-10 10.10.255.7 .1.3.6.1.2.1.17.4.3.1.1

But this code comes up with this error

SNMPv2-SMI::mib-2.17.4.3.1.1 = No Such Instance currently exists at this OID


from pysnmp.hlapi import *
from pysnmp.entity.rfc3413.oneliner import cmdgen

macoid  = '.1.3.6.1.2.1.17.4.3.1.1'

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           UsmUserData('username', 'asdfasdfasdf', 'asdfasdfasdf',
                       authProtocol=usmHMACSHAAuthProtocol,
                       privProtocol=usmDESPrivProtocol),
           UdpTransportTarget(('10.10.255.7', 161)),
           ContextData(contextName='vlan-10'),
           ObjectType(ObjectIdentity(macoid)))
)

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:
        print(' = '.join([x.prettyPrint() for x in varBind]))
etingof commented 5 years ago

One thing is that with pysnmp you are using SNMP GET, while with snmpwalk you do SNMP GETNEXT. That can cause the error you observe.

Otherwise, I'd suggest taking a look at what goes to the wire with tcpdump.

gittyhubbyfrankybobby commented 5 years ago

i changed it to:

from pysnmp.hlapi import *
from pysnmp.entity.rfc3413.oneliner import cmdgen

macoid  = '.1.3.6.1.2.1.17.4.3.1.1'

errorIndication, errorStatus, errorIndex, varBinds = next(
    nextCmd(SnmpEngine(),
           UsmUserData('username', 'asdfasdfasdf', 'asdfasdfasdf',
                       authProtocol=usmHMACSHAAuthProtocol,
                       privProtocol=usmDESPrivProtocol),
           UdpTransportTarget(('10.10.255.7', 161)),
           ContextData(contextName='vlan-10'),
           ObjectType(ObjectIdentity(macoid)))
)

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:
        print(' = '.join([x.prettyPrint() for x in varBind]))

and I get the output of 1.3.6.1.6.3.11.2.1.3.0 or

>>> varBind
ObjectType(ObjectIdentity(<ObjectName value object at 0x7f602f5e06a0 tagSet <TagSet object at 0x7f6034907390 tags 0:0:6> payload [1.3.6.1.2.1.17.4.3.1.1]>), <NoSuchInstance value object at 0x7f602f5e0780 tagSet <TagSet object at 0x7f603487e6d8 tags 128:0:1> subtypeSpec <ConstraintsIntersection object at 0x7f60349072e8 consts <SingleValueConstraint object at 0x7f6034907208 consts b''>> encoding iso-8859-1 payload [No Such Instance...ists at this OID]>)

I'll see if I can do a tcpdump.

gittyhubbyfrankybobby commented 5 years ago

I got it settled with a variant of this

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in nextCmd(SnmpEngine(),
           UsmUserData('username', 'xxxxxxxx', 'xxxxxxxxxxx',
           authProtocol=usmHMACSHAAuthProtocol,
           privProtocol=usmDESPrivProtocol),
           UdpTransportTarget(('10.10.10.10', 161)),
           ContextData(contextName='vlan-10'),
           ObjectType(ObjectIdentity('.1.3.6.1.2.1.17.4.3.1.1')),
           lexicographicMode=False):
    if errorIndication:
        print(errorIndication)
        break
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))
etingof commented 5 years ago

In your version two, I do not understand how you could possibly get no-such-instance response. That should not happen with nextCmd.

Anyway, glad it works now! \o/