Search code examples
pythonipv6

How to get current global IPv6-prefix as a variable in python


how can I get my current global IPv6 prefix in python?

Let's say I have the global IP 2010:a:b:c::400/64 on eth0, how can I get this address in python? And is there a way to split the prefix?


Solution

  • You can use netifaces to do that:

    >>> import netifaces
    >>> addrs=netifaces.ifaddresses('ens3')
    >>> addrs[netifaces.AF_INET6]
    [{'netmask': 'ffff:ffff:ffff:ffff::/64', 'addr': '2001:19f0:4400:4983:5400:ff:fe54:1677'}, {'netmask': 'ffff:ffff:ffff:ffff::/64', 'addr': 'fe80::5400:ff:fe5e:1977%ens3'}]
    

    note that there is a link-local ipv6 address which starts with fe80.