0

I am trying to perform an snmpwalk to get number of errors on each interface of a device (via DNS name).

I can successfully run the following snmpwalk from a Debian box:

snmpwalk -v2c -c public atlanta-r1 1.3.6.1.2.1.2.2.1.14

Results:

IF-MIB::ifInErrors.1 = Counter32: 0<br>
IF-MIB::ifInErrors.2 = Counter32: 0<br>
IF-MIB::ifInErrors.3 = Counter32: 0<br>
....

I am trying to convert this to a Python script using pysnmp. I am having some trouble getting it to work. I keep getting a permission denied when I run it. Can anyone help me out with my code? Thanks

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData('public'),
        cmdgen.UdpTransportTarget(('atlanta-r1', 161)),
        '1.3.6.1.2.1.2.2.1.14',
)

if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            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()))
NREZ
  • 942
  • 9
  • 13
  • Your code is correct. When I run it (replacing hostname with demo.snmplabs.com) it returns some OIDs. What exactly is the error message you are getting? What pysnmp.__version__ you are using? – Ilya Etingof Aug 14 '13 at 08:37
  • Ok I fixed the permissions issue. Now I am getting the following error: Traceback (most recent call last): File "./test.py", line 7, in cmdgen.CommunityData(public), TypeError: __init__() takes at least 3 arguments (2 given) – user2681035 Aug 14 '13 at 17:15
  • You seem to be using an ancient pysnmp version. You could either use cmdgen.CommunityData('my-snmp-agent', 'public') syntax (where the first value does not have any real significance but must be unique per unique community string) or upgrade to the latest pysnmp (which takes just a community name as well). – Pooh Aug 14 '13 at 18:36

1 Answers1

-2

This source code is correct and working on my system by replacing hostname. Try to execute this script with sudo/administrator permissions.

chetan
  • 9
  • 4