[android] Get gateway ip address in android

How to get gateway IP details , There is option using wifimanager but. If there is no wify how to find gateway,dns and other details in android device when connected using usb tethering.

This question is related to android ip gateway

The answer is


Go to terminal

$ adb -s UDID shell
$ ip addr | grep inet 
or
$ netcfg | grep inet

I'm using cyanogenmod 7.2 on android 2.3.4, then just open terminal emulator and type:

$ ip addr show
$ ip route show

DNS server is obtained via

getprop net.dns1 

UPDATE: as of Android Nougat 7.x, ifconfig is present, and netcfg is gone. So ifconfig can be used to find the IP and netmask.


Install terminal emulator app, then to see routing table run iproute from the command prompt. Does not require root permissions. I don't know how to get the DNS server. There's no /etc/resolv.conf file. You can try nslookup www.google.com and see what it reports for your server, but on my phone it reports 0.0.0.0 which isn't too helpful.


This solution will give you the Network parameters. Check out this solution


Try the following:

ConnectivityManager connectivityManager = ...;
LinkProperties linkProperties = connectivityManager.getLinProperties(connectivityManager.GetActiveNetwork());
for (RouteInfo routeInfo: linkProperties.getRoutes()) {
    if (routeInfo.IsDefaultRoute() && routeInfo.hasGateway()) {
        return routeInfo.getGateway();
    }
}

On Android 7 works it:

 ip route get 8.8.8.8

output will be: 8.8.8.8 via gateway...


I wanted to post this answer as an update for users of more recent Android builds (CM11/KitKat/4.4.4). I have not tested any of this with TouchWiz or older Android releases so YMMV.

The following commands can be run in all the usual places (ADB, Terminal Emulator, shell scripts, Tasker).

List all available properties:

getprop

Get WiFi interface:

getprop wifi.interface

WiFi properties:

getprop dhcp.wlan0.dns1
getprop dhcp.wlan0.dns2
getprop dhcp.wlan0.dns3
getprop dhcp.wlan0.dns4
getprop dhcp.wlan0.domain
getprop dhcp.wlan0.gateway
getprop dhcp.wlan0.ipaddress
getprop dhcp.wlan0.mask

The above commands will output information regardless of whether WiFi is actually connected at the time.

Use either of the following to check whether wlan0 is on or not:

ifconfig wlan0

netcfg | awk '{if ($2=="UP" && $3 != "0.0.0.0/0") isup=1} END {if (! isup) exit 1}'

Use either of the following to get the IP address of wlan0 (only if it is connected):

ifconfig wlan0 | awk '{print $3}'

netcfg | awk '/^wlan0/ {sub("(0\\.0\\.0\\.0)?/[0-9]*$", "", $3); print $3}'

Just for thoroughness, to get your public Internet-facing IP address, you're going to want to use an external service. To obtain your public IP:

wget -qO- 'http://ipecho.net/plain'

To obtain your public hostname:

wget -qO- 'http://ifconfig.me/host'

Or to obtain your public hostname directly from your IP address:

(nslookup "$(wget -qO- http://ipecho.net/plain)" | awk '/^Address 1: / { if ($NF != "0.0.0.0") {print $NF; exit}}; /name =/ {sub("\\.$", "", $NF); print $NF; exit}') 2>/dev/null

Note: The aforementioned awk command seems overly complicated only because is able to handle output from various versions of nslookup. Android includes a minimal version of nslookup as part of busybox but there is a standalone version as well (often included in dnsutils).


This seems to work well for me. Tested on Touchwiz 5.1, LineageOS 7.1, and CyanogenMod 11.

ip route list match 0 table all scope global

Gives output similar to this:

default via 192.168.1.1 dev wlan0  table wlan0  proto static