[英]How to decode ip addresses from snmp in python
A python script retrieves ip addresses from a Cisco switch (cdp and 8021x). python脚本从Cisco交换机(cdp和8021x)检索IP地址。 However, the ip addresses are not transmitted in a standard ip address format (xxxx), so I have to convert them. 但是,这些IP地址不是以标准IP地址格式(xxxx)传输的,因此我必须对其进行转换。
For some of the addresses this works as follows 对于某些地址,其工作方式如下
INPUT: [u'\n', u'&', u'\xd1', u'{']
>>> print ord(u'\n'),ord(u'&') , ord(u'\xd1') , ord(u'{')
OUTPUT: 10 38 209 123
The following ip addresses I can easily decode 我可以轻松解码的以下IP地址
[u'\n', u'&', u'\xd1', u'l'] --> 10.38.209.108
[u'\n', u'&', u'\xd1', u'f'] --> 10.38.209.102
[u'\n', u'&', u'\xd1', u'p'] --> 10.38.209.112
[u'\n', u'&', u'\xa9', u'.'] --> 10.38.169.46
[u'\n', u'&', u'\xa9', u'*'] --> 10.38.169.42
[u'\n', u'&', u'\xd1', u'v'] --> 10.38.209.118
[u'\n', u'&', u'\xd1', u'g'] --> 10.38.209.103
[u'\n', u'&', u'\xd1', u'|'] --> 10.38.209.124
[u'\n', u'&', u'\xa9', u','] --> 10.38.169.44
[u'\n', u'&', u'\xd1', u'u'] --> 10.38.209.117
[u'\n', u'&', u'\xa8', u'\x15'] --> 10.38.168.21
But I have unfortunately no idea how I should decode the following ip addresses correctly 但不幸的是,我不知道如何正确解码以下IP地址
[u'\n', u'&', u'\u044a'] --> 10.38.1098 (incorrect)
[u'\n', u'&', u'\u044b'] --> 10.38.1099 (incorrect)
[u'\n', u'&', u'\u0459'] --> 10.38.1113 (incorrect)
[u'\n', u'&', u'\u045b'] --> 10.38.1115 (incorrect)
[u'\n', u'&', u'\u044e'] --> 10.38.1102 (incorrect)
[u'\n', u'&', u'\u044d'] --> 10.38.1101 (incorrect)
[u'\n', u'&', u'\u0445'] --> 10.38.1093 (incorrect)
[u'\n', u'&', u'\u0458'] --> 10.38.1112 (incorrect)
[u'\n', u'&', u'\u045e'] --> 10.38.1118 (incorrect)
[u'\n', u'&', u'\u0446'] --> 10.38.1094 (incorrect)
[u'\n', u'&', u'\u045f'] --> 10.38.1119 (incorrect)
[u'\n', u'&', u'\u0462'] --> 10.38.1122 (incorrect)
Does anyone have an idea how I can decode the third part of the list correctly into the third and fourth octet of the ip address? 有谁知道我如何才能正确地将列表的第三部分解码为ip地址的第三和第四八位字节?
You can encode()
it to bytes and then you will have 4 chars/bytes. 您可以将它encode()
为字节,然后将有4个字符/字节。
a = [u'\n', u'&', u'\u044a']
a = ''.join(a)
b = a.encode('utf-8')
print(b[0], b[1], b[2], b[3])
result: 结果:
10 38 209 138
Or as string 或作为字符串
print('{}.{}.{}.{}'.format(*b))
result: 结果:
"10.38.209.138"
BTW: If you get this data from network/socket then (at least in Python3) you should get it as bytes. 顺便说一句:如果您是从网络/套接字获取此数据的,那么(至少在Python3中)应该以字节为单位获取。
EDIT: full example - more or less universal method 编辑:完整的例子-或多或少的通用方法
data = [
[u'\n', u'&', u'\xd1', u'{'], # --> 10 38 209 123
[u'\n', u'&', u'\xd1', u'l'], # --> 10.38.209.108
[u'\n', u'&', u'\xd1', u'f'], # --> 10.38.209.102
[u'\n', u'&', u'\xd1', u'p'], # --> 10.38.209.112
[u'\n', u'&', u'\xa9', u'.'], # --> 10.38.169.46
[u'\n', u'&', u'\xa9', u'*'], # --> 10.38.169.42
[u'\n', u'&', u'\xd1', u'v'], # --> 10.38.209.118
[u'\n', u'&', u'\xd1', u'g'], # --> 10.38.209.103
[u'\n', u'&', u'\xd1', u'|'], # --> 10.38.209.124
[u'\n', u'&', u'\xa9', u','], # --> 10.38.169.44
[u'\n', u'&', u'\xd1', u'u'], # --> 10.38.209.117
[u'\n', u'&', u'\xa8', u'\x15'], # --> 10.38.168.21
[u'\n', u'&', u'\u044a'], # --> 10.38.1098 (incorrect)
[u'\n', u'&', u'\u044b'], # --> 10.38.1099 (incorrect)
[u'\n', u'&', u'\u0459'], # --> 10.38.1113 (incorrect)
[u'\n', u'&', u'\u045b'], # --> 10.38.1115 (incorrect)
[u'\n', u'&', u'\u044e'], # --> 10.38.1102 (incorrect)
[u'\n', u'&', u'\u044d'], # --> 10.38.1101 (incorrect)
[u'\n', u'&', u'\u0445'], # --> 10.38.1093 (incorrect)
[u'\n', u'&', u'\u0458'], # --> 10.38.1112 (incorrect)
[u'\n', u'&', u'\u045e'], # --> 10.38.1118 (incorrect)
[u'\n', u'&', u'\u0446'], # --> 10.38.1094 (incorrect)
[u'\n', u'&', u'\u045f'], # --> 10.38.1119 (incorrect)
[u'\n', u'&', u'\u0462'], # --> 10.38.1122 (incorrect)
]
for a in data:
if len(a) == 3:
b = ''.join(a).encode('utf-8')
else:
b = [ord(x) for x in a]
print(a, ' bytes:', b[0], b[1], b[2], b[3], ' IP: {}.{}.{}.{}'.format(*b))
result (Linux Mint, Python 3.5.2) 结果(Linux Mint,Python 3.5.2)
['\n', '&', 'Ñ', '{'] bytes: 10 38 209 123 IP: 10.38.209.123
['\n', '&', 'Ñ', 'l'] bytes: 10 38 209 108 IP: 10.38.209.108
['\n', '&', 'Ñ', 'f'] bytes: 10 38 209 102 IP: 10.38.209.102
['\n', '&', 'Ñ', 'p'] bytes: 10 38 209 112 IP: 10.38.209.112
['\n', '&', '©', '.'] bytes: 10 38 169 46 IP: 10.38.169.46
['\n', '&', '©', '*'] bytes: 10 38 169 42 IP: 10.38.169.42
['\n', '&', 'Ñ', 'v'] bytes: 10 38 209 118 IP: 10.38.209.118
['\n', '&', 'Ñ', 'g'] bytes: 10 38 209 103 IP: 10.38.209.103
['\n', '&', 'Ñ', '|'] bytes: 10 38 209 124 IP: 10.38.209.124
['\n', '&', '©', ','] bytes: 10 38 169 44 IP: 10.38.169.44
['\n', '&', 'Ñ', 'u'] bytes: 10 38 209 117 IP: 10.38.209.117
['\n', '&', '¨', '\x15'] bytes: 10 38 168 21 IP: 10.38.168.21
['\n', '&', 'ъ'] bytes: 10 38 209 138 IP: 10.38.209.138
['\n', '&', 'ы'] bytes: 10 38 209 139 IP: 10.38.209.139
['\n', '&', 'љ'] bytes: 10 38 209 153 IP: 10.38.209.153
['\n', '&', 'ћ'] bytes: 10 38 209 155 IP: 10.38.209.155
['\n', '&', 'ю'] bytes: 10 38 209 142 IP: 10.38.209.142
['\n', '&', 'э'] bytes: 10 38 209 141 IP: 10.38.209.141
['\n', '&', 'х'] bytes: 10 38 209 133 IP: 10.38.209.133
['\n', '&', 'ј'] bytes: 10 38 209 152 IP: 10.38.209.152
['\n', '&', 'ў'] bytes: 10 38 209 158 IP: 10.38.209.158
['\n', '&', 'ц'] bytes: 10 38 209 134 IP: 10.38.209.134
['\n', '&', 'џ'] bytes: 10 38 209 159 IP: 10.38.209.159
['\n', '&', 'Ѣ'] bytes: 10 38 209 162 IP: 10.38.209.162
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.