[python]Netifaces module

Netifaces is a python module maintained by Alastair and opalmer, I have been benefited by this module a lot. It is easy to use and there are a lot of possibilities with this module. Here are some examples of usage and finally there will be a code of automatic mac address changer without user to put in mac address and interface name.

The gateways method
This collects the information of the gateway(s), this can be used to get the default gateway of the interface, once the default gateway is obtained, exit interface information will also be present.
Now from here we can get the default gateway ip address as well as the exit interface, to specifically get this information I did this.
Now the tuple of ip address and interface is obtained, from here i can either get the gateway ip address or interface.

The interfaces method
This returns the list of interfaces that are available.

The ifaddresses method
This one is a bit complicated, for the definition you need to read the documentation.

This modules returns the interface ip address, broadcast, netmask and the mac address, in order to use this module the interface name has to be passed in as argument.
From the above example, it is clear that 17 is the layer2 information, while 2 is the ipv4 layer 3 information and 10 is the ipv6 layer 3 information.

So supposed I need to get the mac address and only the ipv4 information, I stored these into two variables layer2 and ipv4_layer3 respectively.
From then I can easily get the mac address and the ipv4 address.

So if I need to get the mac address of the interface where default gateway needs to send packets out, I will need to get the gateways’ interface information then pass into ifaddresses method.

Putting together for an automated mac changer script for linux

from netifaces import ifaddresses, gateways
import subprocess
from sys import exit, platform

def get_internet_link():
    # for now only works for linux
    if platform != 'linux':
        print("This script works with linux only.")
    # get the default gateway ip address and interface name
    default_gateway = gateways().get('default').get(2)
    # if there is default gateway
    if default_gateway:
        # get the ifaddresses of the interface captured from default_gateway.
        intf_details = ifaddresses(default_gateway[1])
        # re-arrange data in dictionary.
        return {
            "mac_addr": intf_details.get(17)[0].get('addr'),
            "ipv4": intf_details.get(2)[0].get('addr'),
            "interface": default_gateway[1]
        print("Looks like there is no default gateway...")

# this is the mac changer function that uses ifconfig command in linux.
def mac_change(interface, new_mac_addr):
    subprocess.call("ifconfig {} down".format(interface), shell=True)
    subprocess.call("ifconfig {} hw ether {}".format(interface, new_mac_addr), shell=True)
    subprocess.call("ifconfig {} up".format(interface), shell=True)

if __name__ == "__main__":
    new_mac = "00:11:22:33:44:55"
    link = get_internet_link()
    mac_change(link.get('interface'), new_mac)
    print("MAC address has changed for {}.\n"
          "Original mac address is {},\n"
          "now changed to {}".format(link['interface'],

Mac changer script in action
The original mac address of eth0.

Then run the script.

The result after the mac address changed.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s