Portable Network Interface Information

0.11.0 · maintenance · verified Fri Apr 10

netifaces is a Python library that provides a portable way to access network interface information, including IP addresses, netmasks, broadcast addresses, and gateway details. It supports various operating systems such as OS X, Linux, and Windows. The current version is 0.11.0, but the original project is no longer actively maintained. Forks exist to provide continued support and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to list network interfaces, retrieve detailed address information for a specific interface, and fetch default gateway settings. Note that the output structure for `ifaddresses` is a dictionary keyed by system-dependent address family constants.

import netifaces

# Get a list of all network interface names
interfaces = netifaces.interfaces()
print(f"Interfaces: {interfaces}")

# Get addresses for a specific interface (e.g., the first one found)
if interfaces:
    first_interface = interfaces[0]
    addresses = netifaces.ifaddresses(first_interface)
    print(f"Addresses for {first_interface}:\n{addresses}")

    # Example: Accessing IPv4 addresses if available
    if netifaces.AF_INET in addresses:
        ipv4_addresses = addresses[netifaces.AF_INET]
        print(f"IPv4 addresses on {first_interface}: {ipv4_addresses}")

# Get default gateway information
gateways = netifaces.gateways()
print(f"Gateways: {gateways}")

view raw JSON →