简体   繁体   English

如何使用python从SNMP获取数据?

[英]How to get data from SNMP with python?

How to get value mac and vlan from fdb table uses python? 如何从fdb表获取值mac和vlan使用python?
In bash snmpwalk work fine: 在bash snmpwalk工作正常:

snmpwalk -v2c -c pub 192.168.0.100 1.3.6.1.2.1.17.7.1.2.2.1.2

pysnmp: pysnmp:

import os, sys
import socket
import random
from struct import pack, unpack
from datetime import datetime as dt

from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto.rfc1902 import Integer, IpAddress, OctetString

ip='192.168.0.100'
community='pub'
value=(1,3,6,1,2,1,17,7,1,2,2,1,2)

generator = cmdgen.CommandGenerator()
comm_data = cmdgen.CommunityData('server', community, 1) # 1 means version SNMP v2c
transport = cmdgen.UdpTransportTarget((ip, 161))

real_fun = getattr(generator, 'getCmd')
res = (errorIndication, errorStatus, errorIndex, varBinds)\
    = real_fun(comm_data, transport, value)

if not errorIndication is None  or errorStatus is True:
       print "Error: %s %s %s %s" % res
else:
       print "%s" % varBinds

output: [(ObjectName(1.3.6.1.2.1.17.7.1.2.2.1.2), NoSuchInstance(''))] 输出:[(ObjectName(1.3.6.1.2.1.17.7.1.2.2.1.2),NoSuchInstance(''))]

import netsnmp

def getmac():
    oid = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.2.1.17.7.1.2.2.1.2'))
    res = netsnmp.snmpgetbulk(oid, Version = 2, DestHost='192.168.0.100',
                           Community='pub')
    return res

print getmac()

output: ('27', '27', '25', '27', '27', '27', '24', '27', '25', '18', '4', '27', '25', '27', '27', '25', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '23', '25', '27', '27', '27', '25', '27', '25', '27', '27', '25', '27', '27', '27', '27', '27', '27', '27', '27', '27', '25', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '25', '25', '25', '7', '27', '27', '9', '25', '27', '20', '19', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '11', '25', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '27', '25', '27', '27', '27', '27', '27', '27', '27', '27', '27', '2', '27', '5', '27', '0', '27', '27', '27', '27', '27') 输出:('27','27','25','27','27','27','24','27','25','18','4','27' ,'25','27','27','25','27','27','27','27','27','27','27','27',' 27','27','27','27','27','27','27','27','23','25','27','27','27' ,'25','27','25','27','27','25','27','27','27','27','27','27',' 27','27','27','25','27','27','27','27','27','27','27','27','27' ,'27','27','27','25','25','25','7','27','27','9','25','27',' 20','19','27','27','27','27','27','27','27','27','27','27','27' ,'27','27','27','11','25','27','27','27','27','27','27','27',' 27','27','27','27','27','27','27','25','27','27','27','27','27' ,'27','27','27','27','2','27','5','27','0','27','27','27',' 27','27')

Firs script(pysnmp) return NoSuchInstance. 第一个脚本(pysnmp)返回NoSuchInstance。 Second script(netsnmp) return list of ports but without mac and vlan. 第二个脚本(netsnmp)返回端口列表但没有mac和vlan。 What wrong? 怎么了?

In the pysnmp example you are doing an SNMPGET (snmpget), not a GETNEXT (snmpwalk). 在pysnmp示例中,您正在执行SNMPGET(snmpget),而不是GETNEXT(snmpwalk)。 If you change, 如果你改变了,

real_fun = getattr(generator, 'getCmd')

to

real_fun = getattr(generator, 'nextCmd')

you will start to see useful results. 你会开始看到有用的结果。

As for the discrepancy you saw in the results between snmpwalk and the python net-snmp bindings result: snmpwalk and snmpbulkget behave differently. 至于你在snmpwalk和python net-snmp绑定结果之间的结果中看到的差异: snmpwalksnmpbulkget表现不同。 If you do an snmpbulkget from the command line with the same options as the snmpwalk you'll receive the same results as your python net-snmp example. 如果您使用与snmpwalk相同的选项从命令行执行snmpbulkget ,您将收到与python net-snmp示例相同的结果。

If you update the following line in your python net-snmp example, 如果您在python net-snmp示例中更新以下行,

res = netsnmp.snmpgetbulk(oid, Version=2, DestHost='192.168.0.100', 
                          Community='pub')

to

res = netsnmp.snmpwalk(oid, Version=2, DestHost='192.168.0.100', 
                       Community='pub')

then you should now get the same list of results from the python net-snmp example as you see when you do an snmpwalk on the command line. 那么你现在应该从python net-snmp示例获得相同的结果列表,就像在命令行上执行snmpwalk时所看到的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM