30

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.

Himanshu
  • 909
  • 3
  • 9
  • 13
  • you can try the solution provided in the link, provided by Muhammad Zeeshan. or you can implement a http trace using the following method...[HTTP Trace](http://developer.android.com/reference/org/apache/http/client/methods/HttpTrace.html#HttpTrace%28java.net.URI%29) – Anoop Chandrika HarisudhanNair Jul 13 '11 at 05:32
  • 1
    You can also use [RoutetoHost](http://developer.android.com/reference/android/net/ConnectivityManager.html#requestRouteToHost%28int,%20int%29) this gives the path to the host, the first IP address would be the Gateway (if no Proxy is used). – Anoop Chandrika HarisudhanNair Jul 13 '11 at 06:21
  • Do research the [Connectivity Manager](http://developer.android.com/reference/android/net/ConnectivityManager.html) – Anoop Chandrika HarisudhanNair Jul 13 '11 at 06:24
  • Actually Gateway, DNS and other details will be available but hidden from mobile user by the mobile operator. You can see the APN settings for more details. – Anoop Chandrika HarisudhanNair Jul 13 '11 at 06:26

9 Answers9

24

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

$ ip addr show
$ ip route show
artm
  • 8,554
  • 3
  • 26
  • 43
user432672
  • 349
  • 2
  • 4
16

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).

Six
  • 5,122
  • 3
  • 29
  • 38
16

On Android 7 works it:

 ip route get 8.8.8.8

output will be: 8.8.8.8 via gateway...

user278801
  • 271
  • 2
  • 4
  • 1
    Thanks! On most linux systems either `netstat -rn` or `ip route show` will work, but on Android only `ip route get` shows the gateway. – tmm1 Apr 07 '18 at 02:43
  • On my case, `ip route show` just output broadcast address, while `ip route get` works – Yeung Jul 28 '21 at 05:32
4

Go to terminal

$ adb -s UDID shell
$ ip addr | grep inet 
or
$ netcfg | grep inet
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Daz
  • 41
  • 1
2

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.

Kevin
  • 2,761
  • 1
  • 27
  • 31
  • What does that have to do with the gateway address? – rakslice Apr 12 '13 at 04:46
  • 3
    "If there is no [Wifi] how to find gateway, dns and other details" from the OP.. None of the other answers provided that detail. – Kevin Apr 21 '13 at 18:23
  • how come this lists DNS servers I don't want my device using (IPv6) and it seems to prefer to use these instead of the dhcp.wlan0.dns* entries it should be using? – Michael Jan 03 '17 at 05:17
  • 1
    @rakslice it doesn't. Some home router's dns and gateway have the same ip address as the router's address. – Aero Wang Jun 02 '17 at 06:08
2

Try the following:

ConnectivityManager connectivityManager = ...;
LinkProperties linkProperties = connectivityManager.getLinkProperties(connectivityManager.GetActiveNetwork());
for (RouteInfo routeInfo: linkProperties.getRoutes()) {
    if (routeInfo.IsDefaultRoute() && routeInfo.hasGateway()) {
        return routeInfo.getGateway();
    }
}
Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
J.Zhao
  • 44
  • 2
  • Call requires API level 29 – whalemare Aug 26 '20 at 06:45
  • The `RouteInfo.hasGateway` call is API level 29, but its possible that `RouteInfo.getGateway` is still useful at API level 21. Also of note, `ConnectivityManager.GetActiveNetwork` is deprecated as of API level 29. – Brent Jan 08 '22 at 00:53
1

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
ravilov
  • 121
  • 2
  • 11
0

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.

biegleux
  • 13,179
  • 11
  • 45
  • 52
0

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

Community
  • 1
  • 1
Muhammad Zeeshan
  • 8,722
  • 10
  • 45
  • 55
  • 3
    Solution you are referring uses wifimanager which does not work when device is not connected to wify. how to find these details when device is connected by usb tethering or through gprs. – Himanshu Jun 09 '11 at 11:19