在android中获取网关ip地址

发布于 2024-11-14 21:11:50 字数 102 浏览 9 评论 0原文

如何获取网关 IP 详细信息,可以选择使用 wifimanager,但是。如果没有 wify,当使用 USB 网络共享连接时,如何在 Android 设备中查找网关、DNS 和其他详细信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

热血少△年 2024-11-21 21:11:51

安装终端模拟器应用程序,然后从命令提示符运行 iproute 来查看路由表。不需要root权限。我不知道如何获取 DNS 服务器。没有 /etc/resolv.conf 文件。您可以尝试nslookup www.google.com并查看它为您的服务器报告的内容,但在我的手机上它报告0.0.0.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.

感悟人生的甜 2024-11-21 21:11:51

该解决方案将为您提供网络参数。查看此解决方案

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

唐婉 2024-11-21 21:11:50

我在android 2.3.4上使用cyanogenmod 7.2,然后打开终端模拟器并输入:

$ ip addr show
$ ip route show

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

$ ip addr show
$ ip route show
ˉ厌 2024-11-21 21:11:50

在 Android 7 上可以正常工作:

 ip route get 8.8.8.8

输出将是:
8.8.8.8 通过网关...

On Android 7 works it:

 ip route get 8.8.8.8

output will be:
8.8.8.8 via gateway...

捶死心动 2024-11-21 21:11:50

我想将此答案作为更新发布给最新 Android 版本 (CM11/KitKat/4.4.4) 的用户。我还没有使用 TouchWiz 或旧版 Android 版本测试过这些,所以 YMMV。

以下命令可以在所有常用位置(ADB、终端仿真器、shell 脚本、Tasker)运行。

列出所有可用属性:

getprop

获取 WiFi 接口:

getprop wifi.interface

WiFi 属性:

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

无论当时 WiFi 是否实际连接,上述命令都会输出信息。

使用以下任一方法来检查 wlan0 是否已打开:

ifconfig wlan0

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

使用以下任一方法来获取 wlan0 的 IP 地址(仅当它已连接时):

ifconfig wlan0 | awk '{print $3}'

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

为了彻底起见,获取您的公共互联网-面对IP地址,您将需要使用外部服务。获取您的公共 IP:

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

获取您的公共主机名:

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

或者直接从您的 IP 地址获取您的公共主机名:

(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

注意:上述 awk 命令看起来过于复杂,只是因为它能够处理各种版本的输出nslookup。 Android 包含最小版本的 nslookup 作为 busybox 的一部分,但也有一个独立版本(通常包含在 dnsutils 中)。

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

阿楠 2024-11-21 21:11:50

前往终端

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

Go to terminal

$ adb -s UDID shell
$ ip addr | grep inet 
or
$ netcfg | grep inet
会发光的星星闪亮亮i 2024-11-21 21:11:50

请尝试以下操作:

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

Try the following:

ConnectivityManager connectivityManager = ...;
LinkProperties linkProperties = connectivityManager.getLinkProperties(connectivityManager.GetActiveNetwork());
for (RouteInfo routeInfo: linkProperties.getRoutes()) {
    if (routeInfo.IsDefaultRoute() && routeInfo.hasGateway()) {
        return routeInfo.getGateway();
    }
}
无人接听 2024-11-21 21:11:50

DNS 服务器是通过

getprop net.dns1 

更新获得的:从 Android Nougat 7.x 开始,ifconfig 存在,而 netcfg 消失了。所以ifconfig可以用来查找IP和网络掩码。

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.

成熟的代价 2024-11-21 21:11:50

这似乎对我来说很有效。在 Touchwiz 5.1、LineageOS 7.1 和 CyanogenMod 11 上进行测试。

ip route list match 0 table all scope global

输出类似于以下内容:

default via 192.168.1.1 dev wlan0  table wlan0  proto static

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文