将Python easysnmp unicode返回转换为十六进制字符串



值从命令行返回,如下所示:

#~ snmpwalk -v 2c -c someComm localhost .1.3.6.1.2.1.123.1.4.1.12 | grep -i "87 54 2A 63"
SNMPv2-SMI::mib-2.123.1.4.1.12.1.8 = Hex-STRING: 87 54 2A 63
SNMPv2-SMI::mib-2.123.1.4.1.12.1.23 = Hex-STRING: 87 54 2A 63
SNMPv2-SMI::mib-2.123.1.4.1.12.1.32 = Hex-STRING: 87 54 2A 63
SNMPv2-SMI::mib-2.123.1.4.1.12.8.3 = Hex-STRING: 87 54 2A 63

但是当我使用easysnmp时,它们会像这样以unicode的形式返回,我无法搜索我需要的行,你能帮我把它转换回来吗?

>>>c = snmp_walk('.1.3.6.1.2.1.123.1.4.1.12', hostname='localhost', community='someComm', version=2)
>>> pprint(c)
[<SNMPVariable value='T*n (contains binary)' (oid='mib-2.123.1.4.1.12.1.1', oid_index='', snmp_type='OCTETSTR')>,
<SNMPVariable value='B& (contains binary)' (oid='mib-2.123.1.4.1.12.1.2', oid_index='', snmp_type='OCTETSTR')>,
...
>>>c= c[1].value
>>> type(c)
<type 'unicode'>
>>> c
u'Bxa2xb6&'
>>> print(c)
B¢¶&

谢谢你的光临!

遵循"如何将int转换为十六进制字符串?"的指导?,我能够将值从unicode转换回十六进制对。以下是一个片段:

>>> myStr = ""
>>> for b in c:
...   myStr += ("%0.2X" % ord(b)) + " "
...
>>> myStr.strip()
'A8 D7 66 BC'

最新更新