4

I want to get snmp data by using python pysnmp module. I was using command line to get SNMP data but now I want to read it using pysnmp module.

SNMP command -

snmpwalk -v 1 -c public <ip address>:<port> xyz::pqr

I was using command like above. Now I tried something like below -

import netsnmp

def getmac():
    oid = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.2.1.17.7.1.2.2.1.2'))
    res = netsnmp.snmpgetbulk(oid, Version = 1, DestHost='ip',
                           Community='pub')
    return res

print getmac()

I'm facing error - import netsnmp. No module netsnmp

Anyone can give me suggestion how I can get snmp data from the snmp server with python?

ketan
  • 2,732
  • 11
  • 34
  • 80

1 Answers1

5

You seem to be using the netsnmp module as opposed to the pysnmp.

If you want to use pysnmp, then this example may help:

from pysnmp.hlapi import *

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in nextCmd(SnmpEngine(),
                          CommunityData('public', mpModel=0),
                          UdpTransportTarget(('demo.snmplabs.com', 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity('1.3.6.1.2.1.17.7.1.2.2.1.2'))):
    if errorIndication or errorStatus:
        print(errorIndication or errorStatus)
        break
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

UPDATE:

The above loop will fetch one OID-value per iteration. If you want to fetch data more efficiently, one option is to stuff more OIDs into the query (in form of many ObjectType(...) parameters).

Or you can switch onto the GETBULK PDU type which can be done by changing your nextCmd call into bulkCmd like this.

from pysnmp.hlapi import *

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in bulkCmd(SnmpEngine(),
        CommunityData('public'),
        UdpTransportTarget(('demo.snmplabs.com', 161)),
        ContextData(),
        0, 25,  # fetch up to 25 OIDs one-shot
        ObjectType(ObjectIdentity('1.3.6.1.2.1.17.7.1.2.2.1.2'))):
    if errorIndication or errorStatus:
        print(errorIndication or errorStatus)
        break
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Keep in mind that GETBULK command support was first introduced in SNMP v2c, that is you can't use it over SNMP v1.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Thanks for your reply. I tried your snippet but all data didn't get retrieved. Any Idea, why like that? – ketan Jun 12 '17 at 10:47
  • @IIya Etingof- How we can retrieve more than one say 10 OID data at a time? – ketan Jun 12 '17 at 12:54
  • @kit Updated my answer, let me know if this is what you need – Ilya Etingof Jun 13 '17 at 07:40
  • @IIya Etingof- If I set more than one ObjectType with more than one OID's I'm not getting any data. How I can make use of GETBULK to do so. please give me sample snippet. – ketan Jun 13 '17 at 08:15
  • @kit added getbulk snippet – Ilya Etingof Jun 13 '17 at 08:37
  • @IIya Etingof- Thanks. This is what I want. – ketan Jun 13 '17 at 08:44
  • @IlyaEtingof short question: it looks like you can indeed have multiple `ObjectType`s objects. Any idea if it's also possible to have multiple `UdpTransportTarget `s? Also, I'd appreciate any feedback on [this question](https://codereview.stackexchange.com/questions/238781/find-neighbours-of-a-switch-using-python-and-snmp-lldp) – Grajdeanu Alex Mar 12 '20 at 18:29
  • You can't have multiple targets within a single SNMP command call (e.g, `bulkCmd`), but you indeed can have many command calls targeting its own target. These calls can be run sequentially (in a loop) or in parallel. For the latter you might want to do it asynchronously ([example](http://snmplabs.com/pysnmp/docs/hlapi/asyncore/manager/cmdgen/bulkcmd.html)). – Ilya Etingof Mar 13 '20 at 10:18