Skip to content

Use ifparser to parse output of ifconfig

  • by

If you want to parse output of ifconfig with Python, you can use ifconfig.

Install: sudo pip install -U ifparser

Usage:

from ifparser import Ifcfg
import commands
ifdata = Ifcfg(commands.getoutput('ifconfig -a'))
# get all the interfaces
ifdata.interfaces
# get details of a particular interface
eth0 = ifdata.get_interface('eth0')
eth0.get_values()
eth0.ip
# parse output on remote system
import paramiko
client = paramiko.SSHClient()
client.connect(hostname, username, password, key_filename)
_, stdout, _ = client.exec_command(command, timeout)
ifdata = Ifcfg(stdout)

For information: https://pydigger.com/pypi/ifparser