ifaddr Library

0.2.0 · active · verified Thu Apr 09

ifaddr is a small, cross-platform Python library designed for enumerating network interfaces and their associated IP addresses. It supports Linux, OS X, Windows, and is expected to work on other BSD derivatives and Solaris/Illumos. Released under the MIT License, the current version 0.2.0 (June 2022) works with Python 3.9+ and requires no compilation, simplifying installation.

Warnings

Install

Imports

Quickstart

This example demonstrates how to retrieve all network adapters and iterate through their configured IP addresses. It also shows how to include unconfigured adapters if needed.

import ifaddr

# Get all network adapters
adapters = ifaddr.get_adapters()

for adapter in adapters:
    print(f"IPs of network adapter {adapter.nice_name}")
    if adapter.ips:
        for ip in adapter.ips:
            # ip.ip will be a string for IPv4, and a tuple (ip_str, flowinfo, scope_id) for IPv6
            print(f"  {ip.ip}/{ip.network_prefix}")
    else:
        print("  No IPs configured")

# To include interfaces without configured IP addresses:
# adapters_with_unconfigured = ifaddr.get_adapters(include_unconfigured=True)

view raw JSON →