Python

Get IP Address, MAC Address and Network Interface Name Using Python : OS Independent

Getting IP Address:

We will make use of Python’s socket module. Here is the brief idea behind my implementation.

  • We will create a socket and instruct it to make use of either IPv4 or IPv6 address and datagram protocol to connect to Google’s DNS server i.e. 8.8.8.8
  • Why Google’s DNS server? Well… it’s always UP or rather we expect it to be always up.
  • Once we connect, then we can retrieve the local address that our socket used to communicate with the DNS server. In this case it will be either your system’s IPv4 or IPv6 address depending on what you have used during the socket creation.

Also please note the following facts before we see the code which gets us the IP address of the machine.

  1. AF_INET is an address family which is used to specify IPv4 i.e. Internet Protocol v4 addresses. Similarly for IPv6 addresses AF_INET6 can be used.
  2. DNS servers use UDP i.e. datagram protocol (WHY? please refer to this post). To connect to a DNS server using a socket you need to use datagram protocol. In python we specify that using SOCK_DGRAM constant.
  3. getsockname() function returns the locally-bound name of the specified socket i.e. the address it has used for connection.

Now, the code.

import socket

#[Comment]: Gets the IPV4 address of the active network adapter.
def get_network_ip_address(self):
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

  #[Comment]: 8.8.8.8 is the primary DNS server for Google DNS. It is assumed that this DNS server will always be up.
  s.connect(("8.8.8.8", 80))
  ip = s.getsockname()[0]
  s.close()
  return ip

If you need IPv6 address, just replace socket.AF_INET with socket.AF_INET6.

Getting Network Interface Name:

We will make use of Python’s psutil module here. Here is the brief idea behind my implementation.

  • First we will retrieve system’s IPv4 address using the above mentioned technique.
  • We will use ‘psutil‘ module to get all available network interfaces on the system in the form of a list of dictionaries. Each Dictionary contains network interface name as ‘Key‘ and interface details object as ‘Value‘.
  • We will browse through the dictionaries one-by-one and find out which network interface object has a matching IPv4 address (Which we would have already retrieved in the first step).
  • After identifying the network interface just return its name.

Here’s the code:

import psutil
import socket

#[Comment]: Gets the active network interface name.
def get_active_network_interface_name(self, ipv4Address):
  nics = psutil.net_if_addrs()
  netInterfaceName = [i for i in nics for j in nics[i] if j.address==ipv4Address and j.family==socket.AF_INET][0]
  return netInterfaceName

If you need to search by IPv6 address, just replace socket.AF_INET with socket.AF_INET6.

Getting MAC address:

Here also we will make use of Python’s psutil module. Here is the brief idea behind my implementation.

  • First we will retrieve system’s active network interface name using the above mentioned technique.
  • We will use ‘psutil‘ module to get all available network interfaces on the system in the form of a list of dictionaries.
  • We will browse through the dictionaries one-by-one and get the network interface details object corresponding to the network interface name (Which we would have already retrieved in the first step).
  • After getting the network interface details object just return its MAC address or physical address.

Also please note the following fact before we see the code which gets us the MAC address of the machine.

  1. AF_LINK is an address family which is used to specify MAC addresses or Physical addresses.

Code:

import psutil

#[Comment]: Gets the physical/MAC Address of the specified network interface.
def get_network_physical_address(self, netInterfaceName):
  nics = psutil.net_if_addrs()
  macAddress = ([j.address for i in nics for j in nics[i] if i==netInterfaceName and j.family==psutil.AF_LINK])[0]
  return macAddress.replace('-',':')

 

Leave a comment