How to get the default gateway via Ethernet, not Wi-Fi

I look forward to how to programmatically find the default gateway address. I already found the default gateway address for Wifi-Manager (getDhcpInfo ()), but I did not find it in Ethernet mode. You are welcome. Help me ~~

0
source share
4 answers

If you like to have two connections at a time, you can go to the Prompt command line and check the advertised routes. If there is one standard gateway on the Route, you can connect to this particular network. If it has two default gateways, you can access a network that is random

thanks bowmi

0
source

, Google-TV, , Google-TV . ? , , , .

0

IP- Ethernet Google TV:

private static final String IPADDRESS_PATTERN =
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en =
                NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr =
                    intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                String ipAddress = inetAddress.getHostAddress().toString();
                if (!inetAddress.isLoopbackAddress()
                        && validate(ipAddress)) {

                    return ipAddress;
                }
            }
        }
    } catch (SocketException e) {
        // TODO(mjoshi): Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}


/**
 * Validate ip address with regular expression
 * 
 * @param ip ip address for validation
 * @return true valid ip address, false invalid ip address
 */
public boolean validate(final String ip) {
    Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
    Matcher  matcher = pattern.matcher(ip);
    return matcher.matches();
}
0

, Able Remote. , , ( Android 2.3 ). , , IP-, :

public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                if (intf.isUp()) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            if (inetAddress instanceof Inet4Address) { // only want ipv4 address
                                return inetAddress.getHostAddress().toString();
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            Log.e(LOG_TAG, "Failed to get the IP address", e);
        }

        return null;
    }
0

Source: https://habr.com/ru/post/1617092/


All Articles