简体   繁体   English

使用python解析snmp walk输出

[英]Parsing snmp walk output using python

Can anyone suggest a good library or a method to parse the output similar to the following snmpwalk output. 任何人都可以建议一个好的库或一种方法来解析输出,类似于以下的snmpwalk输出。

The output is from a juniper box. 输出来自杜松盒。 I want to be able to extract things like the serial numbers and associate with the relevant components 我希望能够提取序列号之类的东西并与相关组件关联

The full output can be viewed here. 完整的输出可以在这里查看。

SOME_DEVICE_NAME,2636.3.1.1.0 : OBJECT IDENTIFIER: .iso.org.dod.internet.private.enterprises.2636.1.1.1.1.40.0
SOME_DEVICE_NAME,2636.3.1.2.0 : OCTET STRING- (ascii):     node1 Juniper SRX650 Internet Router
SOME_DEVICE_NAME,2636.3.1.3.0 : OCTET STRING- (ascii):     AJ5113AK0055
SOME_DEVICE_NAME,2636.3.1.4.0 : OCTET STRING- (ascii):
SOME_DEVICE_NAME,2636.3.1.5.0 : Timeticks: (2147483647) 248 days, 13:13:56.47

I would recommend to use some Python SNMP library to ease parsing the output. 我建议使用一些Python SNMP库来简化输出解析。

You can use eg Net-SNMP Python Bindings . 您可以使用例如Net-SNMP Python Bindings See docs for details. 有关详细信息,请参阅文档

Simplified example of usage: 用法的简化示例:

import netsnmp

session = netsnmp.Session(DestHost="SOME_DEVICE_NAME", Version=2, Community="public")
session.UseLongNames = 1
session.UseNumeric = 1
oid=".1.3.6.1.4.1.2636.3.1"
vars = netsnmp.VarList(oid)
session.walk(vars)

# to check errors: if len(session.ErrorStr) > 0:

# to get output values
for item in vars:
        print item.val

Then you can use some regex parsing etc. 然后,您可以使用一些正则表达式解析等。

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

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