Python SNMP GetBulk — несколько OID

Мне нужно использовать реализацию GetBulk PySNMP для запроса списка OID таблицы. Мне удалось заставить его запросить 1 OID таблицы, но не удалось заставить его прочитать OID таблицы из списка. Что я делаю неправильно? Пожалуйста помоги.

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())

person sonofdelphi    schedule 02.05.2011    source источник


Ответы (2)


Я думаю, это то, что вы хотите...

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
        )

РЕДАКТИРОВАТЬ

С использованием

[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 ~]$

Я получаю...

[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)
>>>
person This    schedule 02.05.2011
comment
Это работало на двоих. Но для 3 это не работает. ,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 ) - person sonofdelphi; 03.05.2011
comment
опубликовали как обновление исходного вопроса. форматирование кода в комментариях кажется невозможным. - person sonofdelphi; 03.05.2011
comment
@sonofdelphi, форматирование кода возможно с помощью backticks around the code, но многострочный код в комментариях невозможен. Пожалуйста, посмотрите мой ответ, я не уверен, что случилось с вашим кодом, но мой, похоже, работает с произвольным количеством проходов oid (до размера ответа 64 КБ) - person This; 03.05.2011

Попробуйте это... может сработать

# 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
person priya    schedule 17.07.2012