Kartik
Kartik

Reputation: 9853

Getting Data from an SNMP port in Python

I am specifically trying to read data from an SNMP port in python using the PySNMP library. I am interested in getting data only through this library. This is because I am making a move from NetSNMP to PySNMP.

This is the working code I had for NetSNMP that actually gives me the data from the port

import netsnmp as snmp

infoSet = snmp.Varbind('1.3.6.1.2.1.123.1.7.1.1.0')
infoGet = snmp.snmpget(infoSet, Version = 1, DestHost = 'localhost', Community = "public")

print infoGet

These 3 lines of code return me the actual reading from that port and I am trying to get the same data from PySNMP and this is the code so far

from pysnmp.entity.rfc3413.oneliner import cmdgen

errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(

cmdgen.CommunityData('my-agent', 'public', 0),
cmdgen.UdpTransportTarget(('localhost', 161)),
(1,3,6,1,2,1,1,1,0)
)

print varBinds[0][0]

This is printing the SNMP address and I am trying to get past this stage where I can get the actual reading for the port mentioned above and I am not sure how to. I am finding it hard to follow through the documentation.

Any help would be appreciated.

Thanks

Upvotes: 3

Views: 4118

Answers (1)

Ilya Etingof
Ilya Etingof

Reputation: 5555

The varBinds is a list of tuples. Each tuple holds a OID-value pair.

>>> print varBinds
[(ObjectName(1.3.6.1.2.1.1.1.0), OctetString('Example Command Responder'))]
>>> print varBinds[0]
(ObjectName(1.3.6.1.2.1.1.1.0), OctetString('Example Command Responder'))
>>> print varBinds[0][0]
1.3.6.1.2.1.1.1.0
>>> print varBinds[0][1]
Example Command Responder

So if all you need is a value, then

>>> print varBinds[0][1]

might help.

Upvotes: 3

Related Questions