android 網絡地址,ip地址獲取,子網掩碼獲取,默認網關獲取

獲取eth0的ip: getIpAddrForInterfaces("eth0")java

    private String getIpAddrForInterfaces(String interfaceName){
        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
                        return inetAddress.getHostAddress();
                    }
//                    判斷未lo時                                          
//                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
//                        return inetAddress.getHostAddress();
//                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

獲取eth0的子網掩碼:getIpAddrMaskForInterfaces("eth0")網絡

 private String getIpAddrMaskForInterfaces(String interfaceName) {
        try {
            Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();    //獲取本機全部的網絡接口
            while (networkInterfaceEnumeration.hasMoreElements()) { //判斷 Enumeration 對象中是否還有數據
                NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); //獲取 Enumeration 對象中的下一個數據
                if (!networkInterface.isUp() && !interfaceName.equals(networkInterface.getDisplayName())) { //判斷網口是否在使用,判斷是否時咱們獲取的網口
                    continue;
                }

                for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {    //
                    if (interfaceAddress.getAddress() instanceof Inet4Address) {    //僅僅處理ipv4
                        return calcMaskByPrefixLength(interfaceAddress.getNetworkPrefixLength());   //獲取掩碼位數,經過 calcMaskByPrefixLength 轉換爲字符串
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "error";
    }

    //經過子網掩碼的位數計算子網掩碼
    public 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;
    }

獲取默認網關:getGateWay() oop

private String getGateWay() {
        String [] arr;
        try {
            Process  process = Runtime.getRuntime().exec("ip route list table 0");
            String data = null;
            BufferedReader ie = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            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 "error";
    }
相關文章
相關標籤/搜索