def parse_ifconfig_output(output, only_active_interfaces=True):
    ifconfig = IfConfig()
    current_interface = None

    for l in output.split('\n'):
        if l:
            # At any line starting with eth0, lo, tap7, etc..
            # Start a new interface.
            if not l[0].isspace():
                current_interface = NetworkInterface(l.split()[0].rstrip(':'))
                ifconfig.interfaces.append(current_interface)
                l = ' '.join(l.split()[1:])

            if current_interface:
                # If this line contains 'inet'
                for inet_addr in re.findall(r'inet (addr:)?(([0-9]*\.){3}[0-9]*)', l):
                    current_interface._ip = inet_addr[1]

    # Return only the interfaces that have an IP address.
    if only_active_interfaces:
        ifconfig._interfaces = filter(lambda i: i.ip, ifconfig._interfaces)

    return ifconfig
Parse the output of an `ifconfig` command.
dataset/ETHPy150Open jonathanslenders/python-deployer/deployer/utils/network.py/parse_ifconfig_output