【问题标题】:Convert Python easysnmp unicode return to hex-string?将 Python easysnmp unicode 返回转换为十六进制字符串?
【发布时间】:2021-03-21 04:03:38
【问题描述】:

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

#~ 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'B\xa2\xb6&'
>>> print(c)
B¢¶&

感谢收看!

【问题讨论】:

  • 你知道u'B\xa2\xb6&amp;'的十六进制数吗?
  • 按 '87 54 2A 63' 顺序排列的东西,4 个十六进制对。

标签: python easysnmp


【解决方案1】:

按照How to convert an int to a hex string? 的指导,我能够将值从 unicode 转换回十六进制对。这是一个sn-p:

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

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    相关资源
    最近更新 更多