1

我正在尝试从 cisco 路由器获取一些 snmp 变量,PySNMP但我使用的是十六进制输出,prettyprint而不是从普通 snmp 浏览器获得的输出。

我尝试了多次编码(hex, utf-8 and ascii)并且没有编码,总是不是我所期望的。

有任何想法吗?谢谢

开始发现10.148.8.15

1.3.6.1.2.1.1.1.0 = 0x436973636f20494f5320536f6674776172652c20494f532d584520536f66747761726520285838365f36345f4c494e55585f494f53442d554e4956455253414c4b392d4d292c2056657273696f6e2031352e3228342953352c2052454c4541534520534f4654574152452028666331290d0a546563686e6963616c20537570706f72743a20687474703a2f2f7777772e636973636f2e636f6d2f74656368737570706f72740d0a436f707972696768742028632920313938362d3230313420627920436973636f2053797374656d732c20496e632e0d0a436f6d70696c6564205475652032352d4665622d31342031313a3336206279206d63707265

   result = {'error': 1, 'value': "default"}
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(community),
cmdgen.UdpTransportTarget((destination, 161)),
cmdgen.MibVariable(oid)

)

# Check for errors and print out results
if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        result['error'] = 1
        # print('%s at %s' % (
        #     errorStatus.prettyPrint(),
        #     errorIndex and varBinds[int(errorIndex)-1] or '?'
        #     )
        # )
    else:
        result['error'] = 0
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
            result['value'] = val.prettyPrint().encode("ascii")
4

1 回答 1

1

这是因为 Cisco SNMP 代理报告控制字符 (\n) 可能会破坏您的脚本输出。有几种方法可以处理这个问题:

  1. 如本例lookupMib=True所示通过。然后 pysnmp 将尝试在 MIB 中查找此特定 OID 的正确输出格式。为此,pysnmp 必须能够找到并加载 pysnmp 格式的 MIB 文件。

  2. 添加一些代码以在看到 0x 前缀时触发十六进制字符串解码器:

    s =     '436973636f...06d63707265'
    
    print(''.join([ chr(int(s[x:x+2], 16)) for x in range(0, len(s), 2) ]))
    

Cisco IOS 软件、IOS-XE 软件 ...由 mcpre 于 2014 年 2 月 25 日星期二 11:36 编译

于 2014-12-29T19:40:14.613 回答