2

I need to use PySNMP's GetBulk implementation to query for a list of Table OIDs. I was able to get it to query for 1 Table OID, but unable to get it to read the Table OIDs from a list. What am I doing wrong? Please help.

from pysnmp.entity.rfc3413.oneliner import cmdgen  

errorIndication, errorStatus, errorIndex, \  
varBindTable = cmdgen.CommandGenerator().bulkCmd(  
            cmdgen.CommunityData('test-agent', 'public'),  
            cmdgen.UdpTransportTarget(('localhost', 161)),  
            0,   
            25,  
            (1,3,6,1,2,1,4,20) # ipAddrTable OID . This works fine.
            # I also want to query .1.3.6.1.2.1.4.21 ipRouteTable in the same command  
            # Putting is as ( (1,3,6,1,2,1,4,20), (1,3,6,1,2,1,4,21) )  gives an error
        )

if errorIndication:
   print errorIndication
else:
    if errorStatus:
    print '%s at %s\n' % (
        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())
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
sonofdelphi
  • 1,986
  • 5
  • 19
  • 25

2 Answers2

1

Try this...it might work

# GETBULK Command Generator
from pysnmp.entity.rfc3413.oneliner import cmdgen
Cn=2
Cr=5
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdgen.CommandGenerator().bulkCmd(
    # SNMP v2
    cmdgen.CommunityData('test-agent', 'public'),
    cmdgen.UdpTransportTarget(('ip-addr', 161)),
    Cn, Cr,
    'oid1','oid2','oid3','oid4'
   )
row=[]
if errorIndication:
    print errorIndication
else:
    if errorStatus:
        print '%s at %s\n' % (
        errorStatus.prettyPrint(),
        errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
        )
    else:
        non_Rep=varBindTable.pop(0)
        for name, val in non_Rep:
            print '%s = %s' % (name.prettyPrint(), val.prettyPrint())
            row.append((name.prettyPrint(), val.prettyPrint()))
        for varBindTableRow in varBindTable:
            varBindTableRow=varBindTableRow[Cn:]
            for name, val in varBindTableRow:
                print '%s = %s' % (name.prettyPrint(), val.prettyPrint())
                row.append((name.prettyPrint(), val.prettyPrint()))
        print row
bluish
  • 26,356
  • 27
  • 122
  • 180
priya
  • 11
  • 1
1

I think this is what you want...

errorIndication, errorStatus, errorIndex, \
varBindTable = cmdgen.CommandGenerator().bulkCmd(
            cmdgen.CommunityData('test-agent', 'public'),
            cmdgen.UdpTransportTarget(('localhost', 161)),
            0,
            25,
            (1,3,6,1,2,1,4,20), # ipAddrTable
            (1,3,6,1,2,1,4,21), # ipRouteTable
        )

EDIT

Using

[mpenning@hotcoffee ~]$ cat sn_GetBulk.py 
from pysnmp.entity.rfc3413.oneliner import cmdgen  

errorIndication, errorStatus, errorIndex, \
varBindTable = cmdgen.CommandGenerator().bulkCmd(  
            cmdgen.CommunityData('test-agent', 'public'),  
            cmdgen.UdpTransportTarget(('192.168.49.49', 161)),  
            0, 
            25, 
            (1,3,6,1,2,1,4,20), # ipAddrTable OID . This works fine.
            (1,3,6,1,2,1,4,21), # ipRouteTable
            (1,3,6,1,2,1,4,22), # ipNetToMediaTable
        )

if errorIndication:
   print errorIndication
else:
    if errorStatus:
        print '%s at %s\n' % (
            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())

[mpenning@hotcoffee ~]$

I receive...

[mpenning@hotcoffee ~]$ python sn_GetBulk.py
1.3.6.1.2.1.4.20.1.1.128.0.0.1 = 128.0.0.1
1.3.6.1.2.1.4.22.1.1.34.192.168.49.1 = 34
1.3.6.1.2.1.4.22.1.1.34.192.168.49.1 = 34
1.3.6.1.2.1.4.20.1.1.128.0.0.16 = 128.0.0.16
1.3.6.1.2.1.4.22.1.1.34.192.168.49.49 = 34
1.3.6.1.2.1.4.22.1.1.34.192.168.49.49 = 34
1.3.6.1.2.1.4.20.1.1.128.0.0.32 = 128.0.0.32
1.3.6.1.2.1.4.22.1.1.38.128.0.0.1 = 38
1.3.6.1.2.1.4.22.1.1.38.128.0.0.1 = 38
1.3.6.1.2.1.4.20.1.1.128.0.0.127 = 128.0.0.127
1.3.6.1.2.1.4.22.1.1.38.128.0.0.16 = 38
1.3.6.1.2.1.4.22.1.1.38.128.0.0.16 = 38
1.3.6.1.2.1.4.20.1.1.192.168.49.49 = 192.168.49.49
1.3.6.1.2.1.4.22.1.1.38.128.0.0.32 = 38
1.3.6.1.2.1.4.22.1.1.38.128.0.0.32 = 38
1.3.6.1.2.1.4.20.1.2.128.0.0.1 = 38
1.3.6.1.2.1.4.22.1.1.502.128.0.0.127 = 502
1.3.6.1.2.1.4.22.1.1.502.128.0.0.127 = 502
1.3.6.1.2.1.4.20.1.2.128.0.0.16 = 38
1.3.6.1.2.1.4.22.1.2.34.192.168.49.1 = '\x00"V\xb8\x1c\xbf'
1.3.6.1.2.1.4.22.1.2.34.192.168.49.1 = '\x00"V\xb8\x1c\xbf'
1.3.6.1.2.1.4.20.1.2.128.0.0.32 = 38
1.3.6.1.2.1.4.22.1.2.34.192.168.49.49 = ',k\xf54=?'
1.3.6.1.2.1.4.22.1.2.34.192.168.49.49 = ',k\xf54=?'
1.3.6.1.2.1.4.20.1.2.128.0.0.127 = 502
1.3.6.1.2.1.4.22.1.2.38.128.0.0.1 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.22.1.2.38.128.0.0.1 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.20.1.2.192.168.49.49 = 34
1.3.6.1.2.1.4.22.1.2.38.128.0.0.16 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.22.1.2.38.128.0.0.16 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.20.1.3.128.0.0.1 = 192.0.0.0
1.3.6.1.2.1.4.22.1.2.38.128.0.0.32 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.22.1.2.38.128.0.0.32 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.20.1.3.128.0.0.16 = 192.0.0.0
1.3.6.1.2.1.4.22.1.2.502.128.0.0.127 = ',k\xf54=\x00'
1.3.6.1.2.1.4.22.1.2.502.128.0.0.127 = ',k\xf54=\x00'
1.3.6.1.2.1.4.20.1.3.128.0.0.32 = 192.0.0.0
1.3.6.1.2.1.4.22.1.3.34.192.168.49.1 = 192.168.49.1
1.3.6.1.2.1.4.22.1.3.34.192.168.49.1 = 192.168.49.1
1.3.6.1.2.1.4.20.1.3.128.0.0.127 = 192.0.0.0
1.3.6.1.2.1.4.22.1.3.34.192.168.49.49 = 192.168.49.49
1.3.6.1.2.1.4.22.1.3.34.192.168.49.49 = 192.168.49.49
1.3.6.1.2.1.4.20.1.3.192.168.49.49 = 255.255.255.0
1.3.6.1.2.1.4.22.1.3.38.128.0.0.1 = 128.0.0.1
1.3.6.1.2.1.4.22.1.3.38.128.0.0.1 = 128.0.0.1
1.3.6.1.2.1.4.20.1.4.128.0.0.1 = 1
1.3.6.1.2.1.4.22.1.3.38.128.0.0.16 = 128.0.0.16
1.3.6.1.2.1.4.22.1.3.38.128.0.0.16 = 128.0.0.16
1.3.6.1.2.1.4.20.1.4.128.0.0.16 = 1
1.3.6.1.2.1.4.22.1.3.38.128.0.0.32 = 128.0.0.32
1.3.6.1.2.1.4.22.1.3.38.128.0.0.32 = 128.0.0.32
1.3.6.1.2.1.4.20.1.4.128.0.0.32 = 1
1.3.6.1.2.1.4.22.1.3.502.128.0.0.127 = 128.0.0.127
1.3.6.1.2.1.4.22.1.3.502.128.0.0.127 = 128.0.0.127
1.3.6.1.2.1.4.20.1.4.128.0.0.127 = 1
1.3.6.1.2.1.4.22.1.4.34.192.168.49.1 = 3
1.3.6.1.2.1.4.22.1.4.34.192.168.49.1 = 3
1.3.6.1.2.1.4.20.1.4.192.168.49.49 = 1
1.3.6.1.2.1.4.22.1.4.34.192.168.49.49 = 4
1.3.6.1.2.1.4.22.1.4.34.192.168.49.49 = 4
1.3.6.1.2.1.4.20.1.5.128.0.0.1 = 65535
1.3.6.1.2.1.4.22.1.4.38.128.0.0.1 = 4
1.3.6.1.2.1.4.22.1.4.38.128.0.0.1 = 4
1.3.6.1.2.1.4.20.1.5.128.0.0.16 = 65535
1.3.6.1.2.1.4.22.1.4.38.128.0.0.16 = 4
1.3.6.1.2.1.4.22.1.4.38.128.0.0.16 = 4
1.3.6.1.2.1.4.20.1.5.128.0.0.32 = 65535
1.3.6.1.2.1.4.22.1.4.38.128.0.0.32 = 4
1.3.6.1.2.1.4.22.1.4.38.128.0.0.32 = 4
1.3.6.1.2.1.4.20.1.5.128.0.0.127 = 65535
1.3.6.1.2.1.4.22.1.4.502.128.0.0.127 = 4
1.3.6.1.2.1.4.22.1.4.502.128.0.0.127 = 4
1.3.6.1.2.1.4.20.1.5.192.168.49.49 = 65535
1.3.6.1.2.1.4.23.0 = 0
1.3.6.1.2.1.4.23.0 = 0
1.3.6.1.2.1.4.22.1.1.34.192.168.49.1 = 34
1.3.6.1.2.1.4.24.3.0 = 6
1.3.6.1.2.1.4.24.3.0 = 6
1.3.6.1.2.1.4.22.1.1.34.192.168.49.49 = 34
1.3.6.1.2.1.4.24.4.1.1.192.168.0.0.255.255.0.0.0.192.168.49.1 = 192.168.0.0
1.3.6.1.2.1.4.24.4.1.1.192.168.0.0.255.255.0.0.0.192.168.49.1 = 192.168.0.0
1.3.6.1.2.1.4.22.1.1.38.128.0.0.1 = 38
1.3.6.1.2.1.4.24.4.1.1.192.168.49.0.255.255.255.0.0.0.0.0.0 = 192.168.49.0
1.3.6.1.2.1.4.24.4.1.1.192.168.49.0.255.255.255.0.0.0.0.0.0 = 192.168.49.0
1.3.6.1.2.1.4.22.1.1.38.128.0.0.16 = 38
1.3.6.1.2.1.4.24.4.1.1.192.168.49.49.255.255.255.255.0.0.0.0.0 = 192.168.49.49
1.3.6.1.2.1.4.24.4.1.1.192.168.49.49.255.255.255.255.0.0.0.0.0 = 192.168.49.49
1.3.6.1.2.1.4.22.1.1.38.128.0.0.32 = 38
1.3.6.1.2.1.4.24.4.1.1.224.0.0.2.255.255.255.255.0.0.0.0.0 = 224.0.0.2
1.3.6.1.2.1.4.24.4.1.1.224.0.0.2.255.255.255.255.0.0.0.0.0 = 224.0.0.2
1.3.6.1.2.1.4.22.1.1.502.128.0.0.127 = 502
1.3.6.1.2.1.4.24.4.1.1.224.0.0.13.255.255.255.255.0.0.0.0.0 = 224.0.0.13
1.3.6.1.2.1.4.24.4.1.1.224.0.0.13.255.255.255.255.0.0.0.0.0 = 224.0.0.13
1.3.6.1.2.1.4.22.1.2.34.192.168.49.1 = '\x00"V\xb8\x1c\xbf'
1.3.6.1.2.1.4.24.4.1.1.224.0.0.22.255.255.255.255.0.0.0.0.0 = 224.0.0.22
1.3.6.1.2.1.4.24.4.1.1.224.0.0.22.255.255.255.255.0.0.0.0.0 = 224.0.0.22
1.3.6.1.2.1.4.22.1.2.34.192.168.49.49 = ',k\xf54=?'
1.3.6.1.2.1.4.24.4.1.2.192.168.0.0.255.255.0.0.0.192.168.49.1 = 255.255.0.0
1.3.6.1.2.1.4.24.4.1.2.192.168.0.0.255.255.0.0.0.192.168.49.1 = 255.255.0.0
1.3.6.1.2.1.4.22.1.2.38.128.0.0.1 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.24.4.1.2.192.168.49.0.255.255.255.0.0.0.0.0.0 = 255.255.255.0
1.3.6.1.2.1.4.24.4.1.2.192.168.49.0.255.255.255.0.0.0.0.0.0 = 255.255.255.0
1.3.6.1.2.1.4.22.1.2.38.128.0.0.16 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.24.4.1.2.192.168.49.49.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.24.4.1.2.192.168.49.49.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.22.1.2.38.128.0.0.32 = '\x00\x0b\xca\xfe\x00\x00'
1.3.6.1.2.1.4.24.4.1.2.224.0.0.2.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.24.4.1.2.224.0.0.2.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.22.1.2.502.128.0.0.127 = ',k\xf54=\x00'
1.3.6.1.2.1.4.24.4.1.2.224.0.0.13.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.24.4.1.2.224.0.0.13.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.22.1.3.34.192.168.49.1 = 192.168.49.1
1.3.6.1.2.1.4.24.4.1.2.224.0.0.22.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.24.4.1.2.224.0.0.22.255.255.255.255.0.0.0.0.0 = 255.255.255.255
1.3.6.1.2.1.4.22.1.3.34.192.168.49.49 = 192.168.49.49
1.3.6.1.2.1.4.24.4.1.3.192.168.0.0.255.255.0.0.0.192.168.49.1 = 0
1.3.6.1.2.1.4.24.4.1.3.192.168.0.0.255.255.0.0.0.192.168.49.1 = 0
1.3.6.1.2.1.4.22.1.3.38.128.0.0.1 = 128.0.0.1
1.3.6.1.2.1.4.24.4.1.3.192.168.49.0.255.255.255.0.0.0.0.0.0 = 0
1.3.6.1.2.1.4.24.4.1.3.192.168.49.0.255.255.255.0.0.0.0.0.0 = 0
1.3.6.1.2.1.4.22.1.3.38.128.0.0.16 = 128.0.0.16
1.3.6.1.2.1.4.24.4.1.3.192.168.49.49.255.255.255.255.0.0.0.0.0 = 0
1.3.6.1.2.1.4.24.4.1.3.192.168.49.49.255.255.255.255.0.0.0.0.0 = 0
[mpenning@hotcoffee ~]$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pysnmp
>>> pysnmp.version
(4, 1, 16)
>>>
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • That worked for two. But it fails for 3. varBindTable = cmdgen.CommandGenerator().bulkCmd( cmdgen.CommunityData('test-agent', 'public'), cmdgen.UdpTransportTarget(('localhost', 161)), 0, 25, (1,3,6,1,2,1,4,20), # ipAddrTable (1,3,6,1,2,1,4,21), # ipRouteTable (1,3,6,1,2,1,4,22), # ipNetToMediaTable ) – sonofdelphi May 03 '11 at 04:58
  • have reposted as update to orig question. code formatting does not seem to be possible in comments. – sonofdelphi May 03 '11 at 05:04
  • @sonofdelphi, code formatting is possible with `backticks around the code`, but multi-line code is impossible in comments. Please see my response, I'm not sure what happened to your code, but mine seems to work with arbitrary numbers of oid walks (up to a response size of 64KB) – Mike Pennington May 03 '11 at 13:55