ifcfg - Python Network Interface Wrapper

0.24 · active · verified Thu Apr 16

Ifcfg is a cross-platform Python library designed for parsing the output of system network configuration commands like `ifconfig` (Unix/Linux/macOS) and `ipconfig` (Windows). It provides a structured way to access network interface information such as IP addresses, netmasks, MAC addresses, and hostnames. The library includes a fallback mechanism to the `ip` command for modern Unix systems where `ifconfig` might be deprecated or unavailable. Currently at version 0.24, it sees active maintenance with several releases per year to address compatibility and add features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart retrieves all network interfaces and prints key details like MAC, IPv4, IPv6, netmask, and MTU. It then attempts to access specific details for a commonly named interface (e.g., 'eth0' on Linux or 'en0' on macOS).

import ifcfg
import json

interfaces = ifcfg.interfaces()

# Print all interfaces and their details
for name, interface_data in interfaces.items():
    print(f"Interface: {name}")
    print(f"  MAC Address: {interface_data.get('ether')}")
    print(f"  IPv4 Addresses: {interface_data.get('inet4')}")
    print(f"  IPv6 Addresses: {interface_data.get('inet6')}")
    print(f"  Netmask: {interface_data.get('netmask')}")
    print(f"  MTU: {interface_data.get('mtu')}")
    print("\n")

# Access a specific interface (e.g., 'eth0' or 'en0')
# Note: Interface names vary by OS and configuration
# Example: Get primary IPv4 for a common interface name
eth0_info = interfaces.get('eth0') or interfaces.get('en0') # Try common names
if eth0_info:
    print(f"Primary IPv4 for 'eth0' or 'en0': {eth0_info.get('inet')}")
    print(f"All IPv4s for 'eth0' or 'en0': {eth0_info.get('inet4')}")
else:
    print("Interface 'eth0' or 'en0' not found.")

view raw JSON →