Returns any IPv4 address of the given network interface, or
null if the interface does not have any IPv4 addresses.
330761
static Inet4Address anyInet4Address(final NetworkInterface interf) {
        return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
            public Inet4Address run() {
                Enumeration<InetAddress> addrs = interf.getInetAddresses();
                while (addrs.hasMoreElements()) {
                    InetAddress addr = addrs.nextElement();
                    if (addr instanceof Inet4Address) {
                        return (Inet4Address)addr;
                    }
                }
                return null;
            }
        });
    }
448959
def interfaces_info():
    
    def replace(value):
        if value == netifaces.AF_LINK:
            return 'link'
        if value == netifaces.AF_INET:
            return 'ipv4'
        if value == netifaces.AF_INET6:
            return 'ipv6'
        return value

    results = {}
    for iface in netifaces.interfaces():
        addrs = netifaces.ifaddresses(iface)
        results[iface] = {replace(k): v for k, v in addrs.items()}

    return results
39027
public static Inet4Address fromIpv4String(String address) {
		byte[] bytes = ip4StringToBytes(address);
		if (bytes == null) {
			return null;
		} else {
			try {
				return (Inet4Address) Inet4Address.getByAddress(bytes);
			} catch (UnknownHostException e) {
				throw new AssertionError(e);
			}
		}
	}
873888
function getOwnIpAddresses() {
    // enumerate interfaces
    var net = os.networkInterfaces();
    var addresses = Object.keys(net)
        // flatten the array structure
        .map(function (k) { return net[k]; })
        .reduce(function (prev, cur) { return prev.concat.apply(prev, cur); }, [])
        // only use external IPv4 ones
        .filter(function (add) { return !add.internal && add.family === "IPv4"; })
        // extract address as number array
        .map(function (k) { return k.address.split(".").map(function (num) { return +num; }); })
        // ignore unconnected ones
        .filter(function (add) { return add[0] !== 169; })
        // turn the address into a string again
        .map(function (a) { return a[0] + "." + a[1] + "." + a[2] + "." + a[3]; });
    return addresses;
}
866363
def _find_interfaces_ip(mac):
    '''
    
    '''
    try:
        mac = napalm_helpers.convert(napalm_helpers.mac, mac)
    except AddrFormatError:
        return ('', '', [])

    all_interfaces = _get_mine('net.interfaces')
    all_ipaddrs = _get_mine('net.ipaddrs')

    for device, device_interfaces in six.iteritems(all_interfaces):
        if not device_interfaces.get('result', False):
            continue
        for interface, interface_details in six.iteritems(device_interfaces.get('out', {})):
            try:
                interface_mac = napalm_helpers.convert(napalm_helpers.mac, interface_details.get('mac_address'))
            except AddrFormatError:
                continue
            if mac != interface_mac:
                continue
            interface_ipaddrs = all_ipaddrs.get(device, {}).get('out', {}).get(interface, {})
            ip_addresses = interface_ipaddrs.get('ipv4', {})
            ip_addresses.update(interface_ipaddrs.get('ipv6', {}))
            interface_ips = ['{0}/{1}'.format(ip_addr,
                                              addr_details.get('prefix_length', '32'))
                             for ip_addr, addr_details in six.iteritems(ip_addresses)]
            return device, interface, interface_ips

    return ('', '', [])
1200450
public IPAddress asAddress(IPVersion version) {
		if(isAddress(version)) {
			return parsedHost.asAddress(version);
		}
		return null;
	}
1013514
def RetrieveIP4Info(self, ip):
    """"""
    if ip.is_private:
      return (IPInfo.INTERNAL, "Internal IP address.")
    try:
      # It's an external IP, let's try to do a reverse lookup.
      res = socket.getnameinfo((str(ip), 0), socket.NI_NAMEREQD)
      return (IPInfo.EXTERNAL, res[0])
    except (socket.error, socket.herror, socket.gaierror):
      return (IPInfo.EXTERNAL, "Unknown IP address.")
941928
public static String getIPAddr() {
		try {
			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
			while (interfaces.hasMoreElements()) {
				NetworkInterface interfaceN = (NetworkInterface) interfaces.nextElement();
				Enumeration<InetAddress> ienum = interfaceN.getInetAddresses();
				while (ienum.hasMoreElements()) {
					InetAddress ipaddr = (InetAddress) ienum.nextElement();
					if (ipaddr instanceof Inet4Address) {
						if (ipaddr.getHostAddress().toString().startsWith("127") || ipaddr.getHostAddress().toString().startsWith("192")) {
							continue;
						} else {
							return ipaddr.getHostAddress();
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
348521
def _findIPAddr(self):
        
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            s.connect(("google.com", 0))
            addr = s.getsockname()[0]
            # print('Using ip : {addr}'.format(addr=addr))
            s.close()
        except socket.error:
            raise NetworkInterfaceException(
                "Impossible to retrieve IP, please provide one manually"
            )
        return addr
1256189
static InetAddress getNIFirstAddress(int niIndex) throws SocketException {
        if (niIndex > 0) {
            NetworkInterface networkInterface = NetworkInterface.getByIndex(niIndex);
            Enumeration<InetAddress> addressesEnum = networkInterface.getInetAddresses();
            if (addressesEnum.hasMoreElements()) {
                return addressesEnum.nextElement();
            }
        }
        return InetAddress.anyLocalAddress();
    }
1023103
public static InetAddress getInetAddress() {
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (IOException ioe) {
            logger.error("Error in getting InetAddress", ioe);
        }
        return inetAddress;
    }
1082959
@Override
  public InetSocketAddress ipLocal()
  {
    Socket s = getSocket();
    
    if (s != null) {
      return (InetSocketAddress) s.getLocalSocketAddress();
    }
    else {
      return null;
    }
  }
457872
def get_host_addresses(use_ipv4, use_ipv6):
    
    addresses = []
    for interface in netifaces.interfaces():
        ifaddresses = netifaces.ifaddresses(interface)
        for address in ifaddresses.get(socket.AF_INET, []):
            if use_ipv4 and address['addr'] != '127.0.0.1':
                addresses.append(address['addr'])
        for address in ifaddresses.get(socket.AF_INET6, []):
            if use_ipv6 and address['addr'] != '::1' and '%' not in address['addr']:
                addresses.append(address['addr'])
    return addresses
939489
def get_ipv4_range(start_addr=None, end_addr=None, return_fields=None, **api_opts):
    '''
    
    '''
    infoblox = _get_infoblox(**api_opts)
    return infoblox.get_range(start_addr, end_addr, return_fields)
666307
def get_if_addr6(iff):
    
    return next((x[0] for x in in6_getifaddr()
                 if x[2] == iff and x[1] == IPV6_ADDR_GLOBAL), None)
377344
def get_by_id(self, id_networkv4):
        

        uri = 'api/networkv4/%s/' % id_networkv4

        return super(ApiNetworkIPv4, self).get(uri)
855842
def get_interface_addresses():
    

    addresses = []
    ifaces = netifaces.interfaces()
    for iface in ifaces:
        addrs = netifaces.ifaddresses(iface)
        families = addrs.keys()

        # put IPv4 to the end so it lists as the main iface address
        if netifaces.AF_INET in families:
            families.remove(netifaces.AF_INET)
            families.append(netifaces.AF_INET)

        for family in families:
            for addr in addrs[family]:
                address = {
                    'name': iface,
                    'family': family,
                    'ip': addr['addr'],
                }
                addresses.append(address)

    return addresses