当前位置:   article > 正文

python snmp 交换机 配置文件_如何使用python从SNMP获取数据?

python通过snmp查看华为交换机内存使用率

如何从fdb表获取值mac和vlan使用python?

在bash snmpwalk工作正常:

snmpwalk -v2c -c pub 192.168.0.100 1.3.6.1.2.1.17.7.1.2.2.1.2

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

输出:[(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()

输出:('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')

第一个脚本(pysnmp)返回NoSuchInstance.第二个脚本(netsnmp)返回端口列表但没有mac和vlan.怎么了?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/156752
推荐阅读
相关标签
  

闽ICP备14008679号