Java IP Address Get getAllInternalHostIPs(String hostName)

Here you can find the source of getAllInternalHostIPs(String hostName)

Description

get All Internal Host I Ps

License

Open Source License

Declaration

public static List<String> getAllInternalHostIPs(String hostName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> getAllInternalHostIPs(String hostName) {
        List<String> ips = null;
        try {// ww  w.j a  v a  2 s .  c o  m
            InetAddress[] addrs = InetAddress.getAllByName(hostName);
            if (null != addrs && addrs.length > 0) {
                ips = new ArrayList<String>();
                for (int i = 0; i < addrs.length; i++) {
                    String ip = addrs[i].getHostAddress();
                    if (isInnerIP(ip))
                        ips.add(ip);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ips;
    }

    /**
     * A=10.0.0.0-10.255.255.255 B=172.16.0.0-172.31.255.255
     * C=192.168.0.0-192.168.255.255
     **/
    public static boolean isInnerIP(String ipAddress) {
        boolean isInnerIp = false;
        long ipNum = getIpNum(ipAddress);
        long aBegin = getIpNum("10.0.0.0");
        long aEnd = getIpNum("10.255.255.255");
        long bBegin = getIpNum("172.16.0.0");
        long bEnd = getIpNum("172.31.255.255");
        long cBegin = getIpNum("192.168.0.0");
        long cEnd = getIpNum("192.168.255.255");
        isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd)
                || ipAddress.equals("127.0.0.1");
        return isInnerIp;
    }

    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[2]);
        long d = Integer.parseInt(ip[3]);

        long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
        return ipNum;
    }

    private static boolean isInner(long userIp, long begin, long end) {
        return (userIp >= begin) && (userIp <= end);
    }
}

Related

  1. getAllHostAddresses(boolean ignoreLoopback, boolean ignoreIP6)
  2. getAllHostIPs(String hostName)
  3. getAllIp()
  4. getAllIPAddresses()
  5. getAllIPsAndAssignedBroadcastAddresses()
  6. getALLLocalHostIP()