def get_iface_addr(iface='eth0', inet_type='AF_INET', inc_aliases=False,
                   fatal=True, exc_list=None):
    """Return the assigned IP address for a given interface, if any.

    :param iface: network interface on which address(es) are expected to
                  be found.
    :param inet_type: inet address family
    :param inc_aliases: include alias interfaces in search
    :param fatal: if True, raise exception if address not found
    :param exc_list: list of addresses to ignore
    :return: list of ip addresses
    """
    # Extract nic if passed /dev/ethX
    if '/' in iface:
        iface = iface.split('/')[-1]

    if not exc_list:
        exc_list = []

    try:
        inet_num = getattr(netifaces, inet_type)
    except AttributeError:
        raise Exception("Unknown inet type '%s'" % str(inet_type))

    interfaces = netifaces.interfaces()
    if inc_aliases:
        ifaces = []
        for _iface in interfaces:
            if iface == _iface or _iface.split(':')[0] == iface:
                ifaces.append(_iface)

        if fatal and not ifaces:
            raise Exception("Invalid interface '%s'" % iface)

        ifaces.sort()
    else:
        if iface not in interfaces:
            if fatal:
                raise Exception("Interface '%s' not found " % (iface))
            else:
                return []

        else:
            ifaces = [iface]

    addresses = []
    for netiface in ifaces:
        net_info = netifaces.ifaddresses(netiface)
        if inet_num in net_info:
            for entry in net_info[inet_num]:
                if 'addr' in entry and entry['addr'] not in exc_list:
                    addresses.append(entry['addr'])

    if fatal and not addresses:
        raise Exception("Interface '%s' doesn't have any %s addresses." %
                        (iface, inet_type))

    return sorted(addresses)
def get_iface_addr ( iface = 'eth0' , inet_type = 'AF_INET' , inc_aliases = False , fatal = True , exc_list = None ) : if '/' in iface : iface = iface . split ( '/' ) [ - 1 ] if not exc_list : exc_list = [ ] try : inet_num = getattr ( netifaces , inet_type ) except AttributeError : raise Exception ( "Unknown inet type '%s'" % str ( inet_type ) ) interfaces = netifaces . interfaces ( ) if inc_aliases : ifaces = [ ] for _iface in interfaces : if iface == _iface or _iface . split ( ':' ) [ 0 ] == iface : ifaces . append ( _iface ) if fatal and not ifaces : raise Exception ( "Invalid interface '%s'" % iface ) ifaces . sort ( ) else : if iface not in interfaces : if fatal : raise Exception ( "Interface '%s' not found " % ( iface ) ) else : return [ ] else : ifaces = [ iface ] addresses = [ ] for netiface in ifaces : net_info = netifaces . ifaddresses ( netiface ) if inet_num in net_info : for entry in net_info [ inet_num ] : if 'addr' in entry and entry [ 'addr' ] not in exc_list : addresses . append ( entry [ 'addr' ] ) if fatal and not addresses : raise Exception ( "Interface '%s' doesn't have any %s addresses." % ( iface , inet_type ) ) return sorted ( addresses )
Return the assigned IP address for a given interface, if any.

    :param iface: network interface on which address(es) are expected to
                  be found.
    :param inet_type: inet address family
    :param inc_aliases: include alias interfaces in search
    :param fatal: if True, raise exception if address not found
    :param exc_list: list of addresses to ignore
    :return: list of ip addresses