简体   繁体   English

在python中打印十六进制字符串格式变量

[英]Printing hex-string format variable in python

I'm retrieving a hex-string format IP address of CDP neighboor from the router using NET-SNMP. 我正在使用NET-SNMP从路由器检索CDP邻居的十六进制格式IP地址。 However, I get instead something weird thing. 但是,我却得到了一些奇怪的东西。 Any help? 有什么帮助吗? Thank you 谢谢

As alecxe mentioned, you really need to provide more details when asking questions. 正如alecxe提到的那样,您在提出问题时确实需要提供更多详细信息。 Fortunately, I've used the NET-SNMP bindings for Python and already know what you are talking about. 幸运的是,我已经将NET-SNMP绑定用于Python,并且已经知道您在说什么。

To convert the binary data you receive from the NET-SNMP bindings to a hex representation, use the Python binascii module. 要将您从NET-SNMP绑定收到的二进制数据转换为十六进制表示,请使用Python binascii模块。 The b2a_hex function is what you are looking for. 您正在寻找b2a_hex函数。

The following is mostly from memory, so it may not be exactly correct, but probably close enough to give you an idea. 以下内容主要来自内存,因此可能并不完全正确,但可能足够接近,足以使您有所了解。

import netsnmp
import binascii

session = netsnmp.Session(Community='public', DestHost='myagentip', Version=1)
vbwithbinarydata = netsnmp.VarBind('oid_to_binary_data')
varlist = netsnmp.Varlist(vbwithbinarydata)
session.get(varlist)
print binascii.b2a_hex(vbwithbinarydata.val)

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

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