简体   繁体   中英

How to decode ip addresses from snmp in python

A python script retrieves ip addresses from a Cisco switch (cdp and 8021x). However, the ip addresses are not transmitted in a standard ip address format (xxxx), so I have to convert them.

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

[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

[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?

You can encode() it to bytes and then you will have 4 chars/bytes.

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.


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)

['\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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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