{"id":8225,"library":"ifcfg","title":"ifcfg - Python Network Interface Wrapper","description":"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.","status":"active","version":"0.24","language":"en","source_language":"en","source_url":"https://github.com/ftao/python-ifcfg","tags":["network","ifconfig","ipconfig","ip","system","interface","cross-platform","networking"],"install":[{"cmd":"pip install ifcfg","lang":"bash","label":"Install ifcfg"}],"dependencies":[],"imports":[{"symbol":"ifcfg","correct":"import ifcfg"}],"quickstart":{"code":"import ifcfg\nimport json\n\ninterfaces = ifcfg.interfaces()\n\n# Print all interfaces and their details\nfor name, interface_data in interfaces.items():\n    print(f\"Interface: {name}\")\n    print(f\"  MAC Address: {interface_data.get('ether')}\")\n    print(f\"  IPv4 Addresses: {interface_data.get('inet4')}\")\n    print(f\"  IPv6 Addresses: {interface_data.get('inet6')}\")\n    print(f\"  Netmask: {interface_data.get('netmask')}\")\n    print(f\"  MTU: {interface_data.get('mtu')}\")\n    print(\"\\n\")\n\n# Access a specific interface (e.g., 'eth0' or 'en0')\n# Note: Interface names vary by OS and configuration\n# Example: Get primary IPv4 for a common interface name\neth0_info = interfaces.get('eth0') or interfaces.get('en0') # Try common names\nif eth0_info:\n    print(f\"Primary IPv4 for 'eth0' or 'en0': {eth0_info.get('inet')}\")\n    print(f\"All IPv4s for 'eth0' or 'en0': {eth0_info.get('inet4')}\")\nelse:\n    print(\"Interface 'eth0' or 'en0' not found.\")","lang":"python","description":"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)."},"warnings":[{"fix":"Ensure that `ifconfig` (or equivalent) or `ip` utilities are installed and accessible in the system's PATH. For Linux, `iproute2` (which provides `ip`) is generally preferred.","message":"Starting with version 0.20, `ifcfg` will raise an exception if neither `ip` nor `ifconfig` commands are found on the system. Prior versions might have silently failed or returned empty results.","severity":"breaking","affected_versions":"0.20 and later"},{"fix":"Upgrade to `ifcfg` 0.21 or newer. If issues persist, verify that your system's `ifconfig` or `ip` commands produce English-like output when run under a 'C' locale (e.g., `LC_ALL=C ifconfig`).","message":"The parsing relies on regex patterns matching the output of `ifconfig`/`ipconfig`/`ip` commands. System locale settings can alter command output, leading to parsing failures or incomplete data. Version 0.21 introduced a fix to force the 'C' locale for command execution, but non-standard system configurations can still cause issues.","severity":"gotcha","affected_versions":"<0.21, and potentially 0.21+ in unusual locale setups"},{"fix":"When retrieving IPv4 addresses, iterate through `interface_data.get('inet4', [])` instead of relying solely on `interface_data.get('inet')`.","message":"The `inet` field (single IPv4 address) is primarily for backward compatibility. Interfaces can have multiple IPv4 addresses. For comprehensive IPv4 address discovery, always use the `inet4` field, which returns a list of all IPv4 addresses for an interface.","severity":"gotcha","affected_versions":"All versions (best practice)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"This often points to an unexpected system command output format. Ensure your system's `ifconfig`/`ip` tools are standard versions. If running on a non-standard Linux/Unix distribution or an older/newer OS version, consider submitting an issue to the `ifcfg` GitHub repository with your `ifconfig`/`ip` output for review. Manually inspecting the raw command output might reveal discrepancies.","cause":"This error indicates that the `ifcfg` parser encountered a line in the `ifconfig` or `ip` command output that it couldn't associate with any known network interface device, likely due to an unexpected output format or malformed data.","error":"RuntimeError: Got results that don't belong to a device"},{"fix":"Install the necessary network utilities for your operating system (e.g., `net-tools` for `ifconfig` or `iproute2` for `ip` on Linux, which is usually installed by default). Ensure the directory containing the command (e.g., `/sbin` or `/usr/sbin`) is in your system's PATH environment variable.","cause":"The system could not find the `ifconfig` (or `ip`/`ipconfig` on other OSes) command in the system's PATH. `ifcfg` relies on executing these external commands to gather network interface information.","error":"OSError: [Errno 2] No such file or directory: 'ifconfig'"},{"fix":"Always use the `.get()` method when accessing interface dictionary keys, providing a default value (e.g., `interface_data.get('inet', 'N/A')` or `interface_data.get('inet4', [])`). If fields are unexpectedly missing, review the system's `ifconfig`/`ip` command output manually for that interface to ensure the data is present and correctly formatted for `ifcfg` to parse.","cause":"Attempting to access a network interface property (e.g., 'inet', 'ether') that either doesn't exist for a particular interface or wasn't parsed successfully from the command output. This can happen if an interface lacks certain attributes (e.g., no IPv4 address) or if the parsing failed due to an unexpected output format.","error":"KeyError: 'inet' or KeyError: 'ether'"}]}