简体   繁体   English

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

[英]Convert Python easysnmp unicode return to hex-string?

The values are coming back from the commandline like this:这些值从命令行返回,如下所示:

#~ 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

But when I use easysnmp, they are coming back as unicode like this and I cannot search for the lines I need, can you help me convert it back?但是当我使用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'B\xa2\xb6&'
>>> print(c)
B¢¶&

Thanks for looking!感谢您的关注!

Following guidance from How to convert an int to a hex string?遵循如何将 int 转换为十六进制字符串的指导? , I was able to convert the values from unicode back to hex pairs. ,我能够将 unicode 中的值转换回十六进制对。 Here is a snippet:这是一个片段:

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

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

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