Operating with IPv6

The next module that we are going to study allows us to manipulate the network address and the interoperability between IPv4 and IPv6. For example, given a certain IP address, we can obtain it in the v4 and v6 formats. The easiest way to install netaddr is to use pip. Download and install the latest version from the Python repository (http://pypi.python.org/pypi/pip) and run the following command:

pip install netaddr

Also, you can see the official source code repository here: https://github.com/drkjam/netaddr.

The following IPAddress object represents a single IP address v6:

>>> from netaddr import *
>>> ipv6 = IPAddress('::1')
>>> ipv6.version
6

We can check whether we have full support for the IPv6 protocol:

>>> ip = IPNetwork('fe80::beef:beef/64')
>>> str(ip), ip.prefixlen, ip.version
('fe80::beef:beef/64', 64, 6)
>>> ip.network, ip.broadcast, ip.netmask, ip.hostmask
(IPAddress('fe80::'), IPAddress('fe80::ffff:ffff:ffff:ffff'), IPAddress('ffff:ffff:ffff:ffff::'), IPAddress('::ffff:ffff:ffff:ffff'))
>>>

Also, we can interoperate between IPv4 and IPv6 with the ipv6() and ipv4() methods:

>>> ip = IPAddress('127.0.0.1').ipv6()
>>> ip
IPAddress('::ffff:127.0.0.1')
>>> ip.ipv4()
IPAddress('127.0.0.1')
>>> ip.ipv6()
IPAddress('::ffff:127.0.0.1')

If we are working with IPv6, it can be interesting that addresses could be compatible also with IPv4:

>>> ip = IPAddress('127.0.0.1').ipv6(ipv4_compatible=True)
>>> ip
IPAddress('::127.0.0.1')
>>> IPAddress('127.0.0.1').ipv6(ipv4_compatible=True).is_ipv4_compat()
True
>>> IPNetwork('::1').ipv6(ipv4_compatible=True)
IPNetwork('::1/128')
>>> IPNetwork('::1').ipv6(ipv4_compatible=True).ipv4()
IPNetwork('0.0.0.1/32')

With this script, we can extract IPv6 information from network interfaces, and with the netaddr package we get information about IP version, IP prefix length, network address, and broadcast address.

You can find the following code in the extract_ipv6_info.py file:

#!/usr/bin/env python3

import socket
import netifaces
import netaddr

def extract_ipv6_info():
print ("IPv6 support built into Python: %s" %socket.has_ipv6)
for interface in netifaces.interfaces():
all_addresses = netifaces.ifaddresses(interface)
print ("Interface %s:" %interface)
for family,addrs in all_addresses.items():
fam_name = netifaces.address_families[family]
for addr in addrs:
if fam_name == 'AF_INET6':
addr = addr['addr']
has_eth_string = addr.split("%eth")
if has_eth_string:
addr = addr.split("%eth")[0]
try:
print (" IP Address: %s" %netaddr.IPNetwork(addr))
print (" IP Version: %s" %netaddr.IPNetwork(addr).version)
print (" IP Prefix length: %s" %netaddr.IPNetwork(addr).prefixlen)
print (" Network: %s" %netaddr.IPNetwork(addr).network)
print (" Broadcast: %s" %netaddr.IPNetwork(addr).broadcast)

except Exception as e:
print ("Skip Non-IPv6 Interface")

if __name__ == '__main__':
extract_ipv6_info()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset