android获取以太网IP、子网掩码、网关、DNS

3,457 阅读1分钟

感谢万能的互联网,本文方法已得到实践应用,真实有效。废话不多说,上代码。(本文只试用ipv4)

1、获取以太网IP地址

public void getIP(){
    WritableMap outMap = Arguments.createMap();

    String interfaceName = "eth0";

    try {
        Enumeration<NetworkInterface> enNetworkInterface = NetworkInterface.getNetworkInterfaces(); //获取本机所有的网络接口
        while (enNetworkInterface.hasMoreElements()) {  //判断 Enumeration 对象中是否还有数据
            NetworkInterface networkInterface = enNetworkInterface.nextElement();   //获取 Enumeration 对象中的下一个数据
            if (!networkInterface.isUp()) { // 判断网口是否在使用
                continue;
            }
            if (!interfaceName.equals(networkInterface.getDisplayName())) { // 网口名称是否和需要的相同
                continue;
            }
            Enumeration<InetAddress> enInetAddress = networkInterface.getInetAddresses();   //getInetAddresses 方法返回绑定到该网卡的所有的 IP 地址。
            while (enInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enInetAddress.nextElement();
                if (inetAddress instanceof Inet4Address) {  //判断是否未ipv4
                    String ip = inetAddress.getHostAddress();
                    return ip;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0.0.0.0";
}

2、获取子网掩码

/*获取子网掩码*/
public static String getSubnetMask() {

    String interfaceName = "eth0";

    try {
        //获取本机所有的网络接口
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        //判断 Enumeration 对象中是否还有数据
        while (networkInterfaceEnumeration.hasMoreElements()) {

            //获取 Enumeration 对象中的下一个数据
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();

            if (!networkInterface.isUp()) {
                // 判断网口是否在使用
                continue;
            }

            if (!interfaceName.equals(networkInterface.getDisplayName())) {
                // 判断是否时我们获取的网口
                continue;
            }

            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                if (interfaceAddress.getAddress() instanceof Inet4Address) {

                    //仅仅处理ipv4
                    //获取掩码位数,通过 calcMaskByPrefixLength 转换为字符串
                    return calcMaskByPrefixLength(interfaceAddress.getNetworkPrefixLength());
                }
            }
        }
    } catch (SocketException e) {

        e.printStackTrace();
    }

    return "0.0.0.0";
}

/*通过子网掩码的位数计算子网掩码*/
private static String calcMaskByPrefixLength(int length) {

    int mask = 0xffffffff << (32 - length);
    int partsNum = 4;
    int bitsOfPart = 8;
    int maskParts[] = new int[partsNum];
    int selector = 0x000000ff;

    for (int i = 0; i < maskParts.length; i++) {

        int pos = maskParts.length - 1 - i;
        maskParts[pos] = (mask >> (i * bitsOfPart)) & selector;
    }

    String result = "";
    result = result + maskParts[0];

    for (int i = 1; i < maskParts.length; i++) {
        result = result + "." + maskParts[i];
    }

    return result;
}

3、获取网关

public static String getGateWay() {
    String[] arr;
    try {
        Process process = Runtime.getRuntime().exec("ip route list table 0");
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String string = in.readLine();
        arr = string.split("\s+");
        return arr[2];
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "0.0.0.0";
}

4、获取DNS

public static String getDns(Context context) {
    /**
     * 获取dns
     */
    String[] dnsServers = getDnsFromCommand();
    if (dnsServers == null || dnsServers.length == 0) {
        dnsServers = getDnsFromConnectionManager(context);
    }
    /**
     * 组装
     */
    StringBuffer sb = new StringBuffer();
    if (dnsServers != null) {
       sb.append(dnsServers[0]); // 可以获取多个DNS,本文只取第一个;读者根据需要可以进行遍历拿取
    }
    return sb.toString();
}


//通过 getprop 命令获取
private static String[] getDnsFromCommand() {
    LinkedList<String> dnsServers = new LinkedList<>();
    try {
        Process process = Runtime.getRuntime().exec("getprop");
        InputStream inputStream = process.getInputStream();
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream));
        String line = null;
        while ((line = lnr.readLine()) != null) {
            int split = line.indexOf("]: [");
            if (split == -1) continue;
            String property = line.substring(1, split);
            String value = line.substring(split + 4, line.length() - 1);
            if (property.endsWith(".dns")
                    || property.endsWith(".dns1")
                    || property.endsWith(".dns2")
                    || property.endsWith(".dns3")
                    || property.endsWith(".dns4")) {
                InetAddress ip = InetAddress.getByName(value);
                if (ip == null) continue;
                value = ip.getHostAddress();
                if (value == null) continue;
                if (value.length() == 0) continue;
                dnsServers.add(value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dnsServers.isEmpty() ? new String[0] : dnsServers.toArray(new String[dnsServers.size()]);
}


private static String[] getDnsFromConnectionManager(Context context) {
    LinkedList<String> dnsServers = new LinkedList<>();
    if (Build.VERSION.SDK_INT >= 21 && context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                for (Network network : connectivityManager.getAllNetworks()) {
                    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
                    if (networkInfo != null && networkInfo.getType() == activeNetworkInfo.getType()) {
                        LinkProperties lp = connectivityManager.getLinkProperties(network);
                        for (InetAddress addr : lp.getDnsServers()) {
                            dnsServers.add(addr.getHostAddress());
                        }
                    }
                }
            }
        }
    }
    return dnsServers.isEmpty() ? new String[0] : dnsServers.toArray(new String[dnsServers.size()]);
}