gpt4 book ai didi

Python Netsnmp 和 snmpwalk

转载 作者:太空狗 更新时间:2023-10-29 12:22:50 25 4
gpt4 key购买 nike

我正在尝试使用 snmpwalk 在某些界面上获取一些信息和统计数据。我用这个:

import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in snmp_res:
print "snmp_res:: ", x

我得到的答案是:

snmp_res:: lo
snmp_res:: EtherNet Adapter XYZ

答案正确,但我需要更多信息。当我用 snmpwalk 从 linux 命令做同样的事情时,我得到了更多信息,例如:

IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: EtherNet Adapter XYZ

“EtherNet Adapter XYZ”的 ID 为 2,我也需要该值来引用界面上的其他统计信息。我如何使用 python 和 snmp 获取它们?

最佳答案

直接来自 the documentation :

snmpwalk(<Varbind/VarList>, <Session args>))

Takes args of netsnmp.Session preceded by a Varbind or VarList from which the 'walk' operation will start. Returns a tuple of values retrieved from the MIB below the Varbind passed in. If a VarList is passed in it will be updated to contain a complete set of VarBinds created for the results of the walk. It is not recommended to pass in just a Varbind since you loose the ability to examine the returned OIDs. But, if only a Varbind is passed in it will be returned unaltered.

Note that only one varbind should be contained in the VarList passed in. The code is structured to maybe handle this is the the future, but right now walking multiple trees at once is not yet supported and will produce insufficient results.

您已经传递了一个VarList,所以您已经拥有了您需要的东西。您只需要正确检查结果即可。

The tests举个例子:

vars = netsnmp.VarList(netsnmp.Varbind('system'))

vals = sess.walk(vars)
print "v1 sess.walk result: ", vals, "\n"

for var in vars:
print " ",var.tag, var.iid, "=", var.val, '(',var.type,')'

关键是修改了input 变量以提供您需要的内容。返回值对你来说没有多大值(value)(笑)。

将所有这些放在一起看起来您想要以下内容:

import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in oid:
print "snmp_res:: ", x.iid, " = ", x.val

(免责声明:无法测试;根据需要进行调整)

该文档中有足够的关于 VarBindVarList 的信息,可以找出最好的东西来摆脱 x .

x.iid但是,是实例标识符,所以它应该给你 12你要找的不要忘记检查 x.tag不过,也可以是 IF-MIB::ifNameIF-MIB::ifDescr (或类似的东西;您必须进行试验)。

关于Python Netsnmp 和 snmpwalk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com