3

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?

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
Vender Aeloth
  • 690
  • 1
  • 12
  • 27

2 Answers2

2

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.

Pooh
  • 276
  • 1
  • 3
1

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

val.prettyPrint().encode("hex")
Community
  • 1
  • 1
Vender Aeloth
  • 690
  • 1
  • 12
  • 27