Introduction to netifaces

If you want to query the network interfaces available on your computer, you can use the netifaces module. We can use a third-party library, netifaces, to find out whether there is IPv6 support on your machine. You can install it with the pip command:

pip install netifaces
For more information, you can explore the netifaces documentation: https://pypi.org/project/netifaces/.

We can call the interfaces() function from this library to list all interfaces present in the system. This script will give a list of all interfaces, and IPv4 and IPv6 addresses available in the system.

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

#!/usr/bin/env python3

import itertools
from netifaces import interfaces, ifaddresses, AF_INET, AF_INET6

def all_interfaces():
for interface in interfaces():
print(ifaddresses(interface))

def inspect_ipv4_addresses():
links = filter(None, (ifaddresses(x).get(AF_INET) for x in interfaces()))
links = itertools.chain(*links)
ip_v4_addresses = [x['addr'] for x in links]
return ip_v4_addresses

def inspect_ipv6_addresses():
links = filter(None, (ifaddresses(x).get(AF_INET6) for x in interfaces()))
links = itertools.chain(*links)
ip_v6_addresses = [x['addr'] for x in links]
return ip_v6_addresses

if __name__ == '__main__':
print(inspect_ipv4_addresses())
print(inspect_ipv6_addresses())
all_interfaces()

In the following script, we are checking whether the Python version supports IPv6 with the has_ipv6 property from the socket package. With the netifaces package, we can get more information for each interface, such as Address family, netmask, and broadcast addresses.

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

#!/usr/bin/env python3

import socket
import netifaces

def inspect_ipv6_support():
print ("IPV6 support built into Python: %s" %socket.has_ipv6)
ipv6_addresses = {}
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]
print (' Address family: %s' % fam_name)

In the previous code block, we used the netifaces module to get interfaces and addresses related with these interfaces. Later, for each IP address we get information about the Address family. Depending the address type, we use an array called ipv6_addresses for store information related with each IP address, such as netmask and broadcast addresses. Finally, we check the ipv6_addresses array for any information about found IPv6 addresses:

            for addr in addrs:
if fam_name == 'AF_INET6':
ipv6_addresses[interface] = addr['addr']
print (' Address : %s' % addr['addr'])
nmask = addr.get('netmask', None)
if nmask:
print (' Netmask : %s' % nmask)
bcast = addr.get('broadcast', None)
if bcast:
print (' Broadcast: %s' % bcast)

if ipv6_addresses:
print ("Found IPv6 address: %s" %ipv6_addresses)
else:
print ("No IPv6 interface found!")

if __name__ == '__main__':
inspect_ipv6_support()

This is the execution of the previous script:

In the execution of the script, we can see that we have three address families listed. AF_LINK is the link layer interface, such as Ethernet, AF_INET is the IPv4 internet address, and AF_INET6 represents the IPv6 internet address.

..................Content has been hidden....................

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