Skip to main content

Python Ifconfig Wrapper for Unix/Linux/MacOSX

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']
    print interface['inet']         # First IPv4 found
    print interface['inet4']        # List of ips
    print interface['inet6']
    print interface['netmask']
    print interface['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
{
    "eth0": {
        "broadcast": "172.16.217.255",
        "ether": "00:0c:29:0c:da:5d",
        "flags": "4163<up,broadcast,running,multicast> ",
        "hostname": "derks-vm.local",
        "inet": "172.16.217.10",
        "inet4": ["172.16.217.10"],
        "inet6": ["fe80::20c:29ff:fe0c:da5d"],
        "mtu": "1500",
        "name": "eth0",
        "netmask": "255.255.255.0",
    },
    "lo": {
        "ether": null,
        "flags": "73<up,loopback,running> ",
        "hostname": "localhost",
        "inet": "127.0.0.1",
        "inet4": ["127.0.0.1"],
        "inet6": ["::1"],
        "mtu": "16436",
        "name": "lo",
        "netmask": "255.0.0.0",
    },
    "virbr0": {
        "broadcast": "192.168.122.255",
        "ether": "52:54:00:5b:70:0d",
        "flags": "4099<up,broadcast,multicast> ",
        "hostname": "derks-vm.local",
        "inet": "192.168.122.1",
        "inet4": ["192.168.122.1"],
        "inet6": [],
        "mtu": "1500",
        "name": "virbr0",
        "netmask": "255.255.255.0",
    }
}

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.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.19.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

ifcfg-0.19-py2.py3-none-any.whl (9.8 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: ifcfg-0.19.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/2.7.15+

File hashes

Hashes for ifcfg-0.19.tar.gz
Algorithm Hash digest
SHA256 4950bc25f55ec73b2c6ab49a10a924e8bdf7afddf2c7477490addd3fc0c3de46
MD5 7fac57ed18c2f0b848456ba95b138c1d
BLAKE2b-256 ce92ab18debaaa19bfcfcbd63df47c16a145811df942cf42a3d8a6c0c686b905

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ifcfg-0.19-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/2.7.15+

File hashes

Hashes for ifcfg-0.19-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 828b8fdcaa0e75a03447964824efd61b268417ab307af063a56cc53e63d1358a
MD5 1c07acbf3665b23c4f55415130c3995a
BLAKE2b-256 6392f60b2568f1ab73324929b1cd76b35fb9a36b47da8debbb6f3b506eed296a

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