Skip to main content

Python ifconfig wrapper for Unix/Linux/MacOSX + ipconfig for Windows

Project description

https://badge.fury.io/py/ifcfg.svg https://travis-ci.org/ftao/python-ifcfg.svg http://codecov.io/github/ftao/python-ifcfg/coverage.svg?branch=master

Ifcfg is a cross-platform (Windows/Unix) library for parsing ifconfig and ipconfig output in Python. It is useful for pulling information such as IP, Netmask, MAC Address, Hostname, etc.

A fallbacks to ip is included for newer Unix systems w/o ifconfig. Windows systems are supported (in English) through ipconfig.

Usage

import ifcfg
import json

for name, interface in ifcfg.interfaces().items():
    # do something with interface
    print interface['device']       # Device name
    print interface['inet']         # First IPv4 found
    print interface['inet4']        # List of ips
    print interface['inet6']        # List of ips
    print interface['netmask']      # Backwards compat: First netmask
    print interface['netmasks']     # List of netmasks
    print interface['broadcast']    # Backwards compat: First broadcast
    print interface['broadcasts']   # List of broadcast

default = ifcfg.default_interface()

The output of ‘ifcfg.interfaces()’ dumped to JSON looks something like the following:

$ python -m ifcfg.cli | python -mjson.tool
{
    "docker0": {
        "inet": "172.17.0.1",
        "inet4": [
            "172.17.0.1"
        ],
        "ether": "01:02:9d:04:07:e3",
        "inet6": [],
        "netmask": "255.255.0.0",
        "netmasks": [
            "255.255.0.0"
        ],
        "broadcast": "172.17.255.255",
        "broadcasts": [
            "172.17.255.255"
        ],
        "prefixlens": [],
        "device": "docker0",
        "flags": "4099<UP,BROADCAST,MULTICAST> ",
        "mtu": "1500"
    },
    "enp0s25": {
        "inet": null,
        "inet4": [],
        "ether": "a0:88:b4:3d:67:7b",
        "inet6": [],
        "netmask": null,
        "netmasks": [],
        "broadcast": null,
        "broadcasts": [],
        "prefixlens": [],
        "device": "enp0s25",
        "flags": "4099<UP,BROADCAST,MULTICAST> ",
        "mtu": "1500"
    },
    "lo": {
        "inet": "127.0.0.1",
        "inet4": [
            "127.0.0.1"
        ],
        "ether": null,
        "inet6": [
            "::1"
        ],
        "netmask": "255.0.0.0",
        "netmasks": [
            "255.0.0.0"
        ],
        "broadcast": null,
        "broadcasts": [
            null
        ],
        "prefixlens": [
            "128"
        ],
        "device": "lo",
        "flags": "73<UP,LOOPBACK,RUNNING> ",
        "mtu": "65536"
    },
}

Development

To bootstrap development, use a Python virtual environment, and install the dev requirements:

# Install dev dependencies
pip install -r requirements_dev.txt
# Run tests locally
make test

You can also install tox and run the tests in a specific environment:

pip install tox
tox -e py27

Before commiting and opening PRs, ensure that you have pre-commit hooks running:

pip install pre-commit
pre-commit install

Release notes

0.24

  • add support for arrow notation inet #71

0.23

  • Add support for multiple netmasks, broadcast addresses, as well as ipv6 prefix lengths #67

0.22

  • Python 3.7 and 3.8 support #51 #53

  • Default interface detection on Windows #25 #56

  • New flags for unix ip command #61

0.22

  • Python 3.7 and 3.8 support #51 #53

  • Default interface detection on Windows #25 #56

  • New flags for unix ip command #61

0.21

  • Force C as locale for running commands, to ensure consistent regex patterns #47

0.20

  • Throw an exception when neither ip nor ifconfig commands exist #45

0.19

  • Adds support for interfaces with VLAN notation, e.g. eth2.2 #40

  • Fetch MTU values from ip command results #39

0.18

  • Adds support for interfaces with non-alphanumeric characters, e.g. eth-int #35 and #36

0.17

  • Restore ip after regressions + add tests

  • Add MacOSX support for ip command

0.16

  • Support for multiple IPv4 addresses in the new ‘inet4’ field

  • Packaging cleanup

0.15

  • Support for bridged interface names #24

0.14

  • Replace Python 2 syntax #21

0.13

  • Further crashes on non-English Windows systems #17

  • Known issue: Localized non-English Windows parsing does not work #18

0.12

  • Fix encoding crashes on non-English Windows systems

0.11

After 6 beta releases, we move on from an idea that this is beta software and instead consider it to be stable – we will probably never actually keep up with all the various ways of detecting network properties for different systems. Anything that is incorrectly detected and can be updated, can also be implemented and shipped as a new patch release.

So let’s ship early, ship often instead.

This release seeks to clean up the codebase (sparingly!) and introduce Windows compatibility.

  • Add Windows compatible parsing of ipconfig output

  • Handle non-unicode terminals (Windows+Mac especially)

  • Removing ill-defined encoding keyword arg from ifcfg.get_parser

  • Removed no-op Linux Kernel 2.x parsing and kernel keyword arg

  • Removed class ifcfg.IfcfgParser, use UnixParser instead

  • All strings are UTF-8, also in Py 2.7

  • Only cross-platform features are now guaranteed to be in the result set: ['inet', 'ether', 'inet6', 'netmask']

  • IPv6 addresses are now stored in a list.

  • Removed prefixlen and scopeid, as they should be added for each IPv6 address, not the interface

  • Allow ifcfg to be imported despite whether or not the OS system is recognized.

  • Remove ifcfg.exc module

  • Fix some interface names containing :_- characters on Linux (Sergej Vasiljev)

0.10.1

  • Fixed encoding issues, preventing default_interface to be detected

0.10

  • Support for Unix systems w/o ifconfig, for instance newer Ubuntu/Debian

  • Refactored to use src/ hierarchy

License

The Ifcfg library is Open Source and is distributed under the BSD License (three clause). Please see the LICENSE file included with this software.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ifcfg-0.24.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

ifcfg-0.24-py2.py3-none-any.whl (10.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ifcfg-0.24.tar.gz.

File metadata

  • Download URL: ifcfg-0.24.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ifcfg-0.24.tar.gz
Algorithm Hash digest
SHA256 64f60f7a0d15c85876b76b5f79246ed8c27bf5f79e5753ec88287145884661ab
MD5 44ddc19905c76efc29658832ab99b350
BLAKE2b-256 ba5153de897f3be2402b53940cfe2570a1838ef828f416b5198dd454f1654b76

See more details on using hashes here.

File details

Details for the file ifcfg-0.24-py2.py3-none-any.whl.

File metadata

  • Download URL: ifcfg-0.24-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ifcfg-0.24-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 950f31ddc3fdd877724fb4154b744434471106088ecf10170b96063c24b3c0b7
MD5 78ae0e3fa525b4692c38cee962ffc224
BLAKE2b-256 d835d5d8da34881946022e311693a875e2d162045f20ea17d7d33707ab929eb8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page