Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PySNMP change output format

Tags:

python

pysnmp

For better performance I have to migrate my bash script to python script... So I begin to use pysnmp and I'm facing an issue about output format...

You will find below netsnmp request:

snmpwalk -v 2c -c mycommunity 192.168.2.20 1.3.6.1.4.1.9.9.387.1.7.8.1.3

The same thing with pysnmp:

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData('mycommunity'),
        cmdgen.UdpTransportTarget(('192.168.2.20', 161)),
        '1.3.6.1.4.1.9.9.387.1.7.8.1.3'
)

With netsnmp I can change the output format like this:

snmpwalk -v 2c -Oa -c mycommunity 192.168.2.20 1.3.6.1.4.1.9.9.387.1.7.8.1.3

But I'm not able to change output format with pysnmp. How I can do it?

like image 861
Vender Aeloth Avatar asked Dec 31 '25 14:12

Vender Aeloth


2 Answers

Response values are all in the VarBindTable so you could format it as you wish. For example:

>>> varBindTable
[ [(ObjectName('1.3.6.1.2.1.1.1.0'), OctetString('SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'))] ]
>>> varBindTable[0][0][1]
OctetString('SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m')
>>> varBindTable[0][0][1].asOctets()
'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
>>> varBinds[0][1].asOctets()

There's no built-in option in pysnmp to modify output format to some pre-defined settings.

like image 183
Pooh Avatar answered Jan 02 '26 05:01

Pooh


I didn't use Pooh's solution but it helped me to find what to do:

val.prettyPrint().encode("hex")
like image 45
Vender Aeloth Avatar answered Jan 02 '26 04:01

Vender Aeloth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!