ocp1000
ocp1000

Reputation: 601

Convert IPv6 to hex notation

My interface is setup:

iface eth1 inet6 static
    address 0:0:0:0:0:ffff:6464:640a
    netmask 64

but when running ifconfig eth1 you see the following configuration:

eth1  inet6 addr: 100.100.100.10/64 Scope:Global

This is because 0:0:0:0:0:ffff:6464:640a translates into 100.100.100.10/64.

I have a code in python that retreives the IP Addresses from each interface:

import netifaces
interface_info = netifaces.ifaddresses('eth1')

if netifaces.AF_INET6 in interface_info:
   return interface_info[netifaces.AF_INET][0]['addr']

The above code returns the IPv6 of eth1 as ::ffff:100.100.100.10.
For my code, I need that the IPv6 will be the original hex notation, i.e., ::ffff:6464:640a and not ::ffff:100.100.100.10.

Any idea how to achieve this?

Upvotes: 1

Views: 4486

Answers (1)

unutbu
unutbu

Reputation: 881037

Using the ipaddress module:

In [54]: import ipaddress

In [55]: addr = '::ffff:100.100.100.10'

In [59]: ipaddress.ip_address(addr)
Out[59]: IPv6Address('::ffff:6464:640a')

In [60]: str(ipaddress.ip_address(addr))
Out[60]: '::ffff:6464:640a'

Upvotes: 3

Related Questions