How to get all network interface cards' information in Python

We can retrieve all network interface cards’ (NIC) information using the psutil module in Python.

The psutil module

psutil (Python system and process utilities) is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). It is mostly used for system monitoring, profiling, restricting process resources, and process management.

The module can be installed via pip as follows:

pip install psutil

The net_if_stats method

The net_if_stats method retrieves information about each NIC as a dictionary. The key is the NIC name, and the value is a named tuple with the following attributes:

  • isup: This is a Boolean value that indicates whether the NIC is up and running.
  • duplex: This refers to the duplex communication type. The possible values are NIC_DUPLEX_FULL, NIC_DUPLEX_HALF, and NIC_DUPLEX_UNKNOWN.
  • speed: This is the speed of the NIC expressed in megabits.
  • mtu: This is the maximum transmission unit of the NIC expressed in bytes.

Syntax

psutil. net_if_stats()

The method has no parameters.

Code

import psutil
print("NIC info:")
print(psutil.net_if_stats())

Code explanation

  • Line 1: We import the psutil module.
  • Line 4: We retrieve information about network interface cards using the psutil.net_if_stats() method.

Free Resources