So you may have looked at /proc/net/route before and thought how the heck am I suppose to read this. Well here is the low down.
This file uses endianness to store the addresses as hexadecimal,
in reverse; for example 192 as hex is C0:
In : hex(192) Out: '0xc0'
So lets take a look at our route file:
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT eth0 00087F0A 00000000 0001 0 0 0 00FFFFFF 0 0 0 eth0 0000FEA9 00000000 0001 0 0 1002 0000FFFF 0 0 0 eth0 00000000 01087F0A 0003 0 0 0 00000000 0 0 0
Now the first entry has a destination of 00087F0A, lets go ahead and chunk these in to hex
In : x = iter('00087F0A') In : res = [ ''.join(i) for i in zip(x, x) ] In : res Out: ['00', '08', '7F', '0A']
Now if we wanted we could convert these hex codes manually:
In : int('0A', 16) Out: 10
But we want to do this in one big swoop:
In : d = [ str(int(i, 16)) for i in res ] In : d Out: ['0', '8', '127', '10']
And there we go, our IP address; however, it appears to be backwards. Lets go ahead and fix that, and return as a string:
In : '.'.join(d[::-1]) Out: '10.127.8.0'
And there we have it! Your function may look something like this when all said and done:
In : def hex_to_ip(hexaddr): ....: x = iter(hexaddr) ....: res = [str(int(''.join(i), 16)) for i in zip(x, x)] ....: return '.'.join(res[::-1]) ....:
And with output like so:
In : hex_to_ip('00087F0A') Out: '10.127.8.0' In : hex_to_ip('0000FEA9') Out: '169.254.0.0'