Java Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

The list of methods to do IP Address Get are organized into topic(s).

Method

StringgetIPByInterfaceName(String name)
Find the IPv4 address applicable to a given interface name
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
Optional<NetworkInterface> device = Collections.list(nets).stream()
        .filter(i -> i.getName().equalsIgnoreCase(name)).findFirst();
if (!device.isPresent())
    return null;
return Collections.list(device.get().getInetAddresses()).stream().filter(v -> v instanceof Inet4Address)
        .map(v -> v.getHostAddress()).findFirst().get();
StringgetIpByName(String name)
Calcula la ip a partir del nombre
InetAddress[] address;
try {
    address = InetAddress.getAllByName(name);
} catch (UnknownHostException e) {
    e.printStackTrace();
    return null;
int i = 0;
...
ListgetIpByNetworkInterfaceName(String name)
get Ip By Network Interface Name
List<String> resultList = null;
NetworkInterface networkInterface = null;
for (int i = 0; i < getUsualNetworkInterfaces().size(); i++) {
    networkInterface = getUsualNetworkInterfaces().get(i);
    if (name.equals(networkInterface.getName())) {
        resultList = getInetAddressList(networkInterface);
return resultList;
byte[]getIPFromHash(final long ipHash)
get IP From Hash
final byte[] ip;
if (isIPv6(ipHash)) {
    ip = new byte[] { (byte) (ipHash & 0xFF), (byte) (ipHash >> 8 & 0xFF), (byte) (ipHash >> 16 & 0xFF),
            (byte) (ipHash >> 24 & 0xFF), (byte) (ipHash >> 32 & 0xFF), (byte) (ipHash >> 40 & 0xFF) };
} else {
    ip = new byte[] { (byte) (ipHash & 0xFF), (byte) (ipHash >> 8 & 0xFF), (byte) (ipHash >> 16 & 0xFF),
            (byte) (ipHash >> 24 & 0xFF) };
return ip;
StringgetIPFromInterface(String ni)
get IP From Interface
if (ni.equals("lo")) {
    return InetAddress.getLocalHost().getHostAddress();
Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
for (; n.hasMoreElements();) {
    NetworkInterface e = n.nextElement();
    if (e.getDisplayName().equals(ni)) {
        return getIPAddress(e);
...
StringgetIPFromNetworkInterface()
Returns the IP address determined from the network interfaces (using the IP address of the one with a proper host name).
String result;
List<String> list;
Enumeration<NetworkInterface> enmI;
NetworkInterface intf;
Enumeration<InetAddress> enmA;
InetAddress addr;
boolean found;
result = null;
...
StringgetIpHostnameLocal()
get Ip Hostname Local
try {
    InetAddress ip = InetAddress.getLocalHost();
    return ip.getHostName();
} catch (Exception e) {
    return "not-found";
SetgetIPList(String address)
get IP List
Set<String> ips = new HashSet<>();
try {
    InetAddress[] machines = InetAddress.getAllByName(address);
    for (InetAddress machine : machines) {
        ips.add(machine.getHostAddress());
} catch (UnknownHostException e) {
return ips;
String[]getIPList(String s, String s1)
get IP List
String[] as = null;
long l = getAddrLong("255.255.255.255");
long l1 = getAddrLong(s1);
if (l1 == l) {
    as = new String[1];
    as[0] = s;
    return as;
long l2 = getAddrLong(s);
long l3 = l ^ l1;
if (l1 == 0L || l2 == 0L || l1 > l || l3 > l) {
    return null;
if (l3 > 0x10000L) {
    l3 = 65025L;
if (l3 == 2L) {
    as = new String[1];
    as[0] = convertAddr(l2 + 1L);
    return as;
if ((l2 + l3) - 1L > l) {
    return null;
as = new String[(int) l3 - 1];
for (int i = 0; (long) i < l3 - 1L; i++) {
    as[i] = convertAddr(l2 + 1L + (long) i);
return as;
StringgetIPs()
get I Ps
StringBuilder v_Ret = new StringBuilder();
Enumeration<?> v_NetInterfaces = null;
InetAddress v_IP = null;
try {
    v_NetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (java.net.SocketException e) {
    return v_Ret.toString();
while (v_NetInterfaces.hasMoreElements()) {
    NetworkInterface v_NetInterface = (NetworkInterface) v_NetInterfaces.nextElement();
    Enumeration<?> v_Addresses = v_NetInterface.getInetAddresses();
    int v_Count = 0;
    v_Ret.append(v_NetInterface.getName()).append("=");
    while (v_Addresses.hasMoreElements()) {
        v_IP = (InetAddress) v_Addresses.nextElement();
        if (v_IP != null && v_IP instanceof Inet4Address) {
            v_Ret.append(v_IP.getHostAddress());
            v_Count++;
            if (v_Count > 1) {
                v_Ret.append(";");
    v_Ret.append(" ");
return v_Ret.toString().trim();