def get_port_by_ip(cfg_facts, ipaddr):
    """
    Returns the port which has a given IP address from the dut config.

    Args:
        cfg_facts: The "ansible_facts" output from the duthost "config_facts" module.
        ipaddr: The IP address to search for.

    Returns:
        A string with the port name or None if not found.  ("Ethernet12")

    """
    if ':' in ipaddr:
        iptype = "ipv6"
    else:
        iptype = "ipv4"

    intf = {}
    intf.update(cfg_facts['INTERFACE'])
    if "PORTCHANNEL_INTERFACE" in cfg_facts:
        intf.update(cfg_facts['PORTCHANNEL_INTERFACE'])
    for a_intf in intf:
        for addrs in intf[a_intf]:
            intf_ip = addrs.split('/')
            if iptype == 'ipv6' and ':' in intf_ip[0] and intf_ip[0].lower() == ipaddr.lower():
                return a_intf
            elif iptype == 'ipv4' and ':' not in intf_ip[0] and intf_ip[0] == ipaddr:
                return a_intf

    raise Exception("Dod not find port for IP %s" % ipaddr)