Android 使用 adb shell 连接到受密码保护的 wifi 网络

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22825443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:28:41  来源:igfitidea点击:

Connect to password protected wifi network using adb shell

androidnetworkingwifiadbaccess-point

提问by Grayson Henry

I have an Android phone, and the goal is to connect the phone to a password protected wifi network.

我有一部 Android 手机,目标是将手机连接到受密码保护的 wifi 网络。

Steps I know so far:

到目前为止我知道的步骤:

adb root
adb shell svc wifi enable

Ok sweet, wifi is turned on. Now I need to connect the phone a certain wireless network that requires a password. I am hoping I can connect using an adb shell command.

好的,亲爱的,wifi 已打开。现在我需要将手机连接到需要密码的某个无线网络。我希望我可以使用 adb shell 命令进行连接。

Any help?

有什么帮助吗?

I would rather not download programs onto the device

我宁愿不将程序下载到设备上

回答by Andrejs Cainikovs

This is possible to achieve using wpa_cli, command line interface of wpa_supplicant:

这可以通过使用以下wpa_cli命令行界面来实现wpa_supplicant

# Get to the shell
adb root
adb shell

# Get to wpa_cli prompt
wpa_cli -p /data/misc/wifi/sockets/ -i wlan0

# Add new WiFi network
add_network
set_network 0 auth_alg OPEN
set_network 0 key_mgmt WPA-PSK
set_network 0 ssid "network_name"
set_network 0 proto RSN
set_network 0 mode 0
set_network 0 psk "password"

# Connect to it
select_network 0
enable_network 0
reassociate

# Check the status
status

In the above list of commands, add_networkcommand will output the index of the new network, which should be used for the subsequent commands. In this example, this index is 0.

在上面的命令列表中,add_networkcommand 会输出新网络的索引,用于后续命令。在这个例子中,这个索引是0

回答by MDR

Use this procedure [more details included :) ]

使用此程序 [包括更多详细信息:)]

1- Make sure wpa_supplicant is running. Look for its pid using this command:

1- 确保 wpa_supplicant 正在运行。使用以下命令查找其 pid:

pidof wpa_supplicant

This command should return the pid of wpa_supplicant process. If nothing returned, wpa_supplicant is not running. Use svc command to turn off wifi and then turned it on again:

此命令应返回 wpa_supplicant 进程的 pid。如果没有返回任何内容,则 wpa_supplicant 未运行。使用 svc 命令关闭 wifi,然后再次打开它:

svc wifi disable
svc wifi enable

2- Read control interfacedirectory from wpa_supplicant.conf file. This file usually exists in /data/misc/wifi/. Open this file using cat command:

2-从 wpa_supplicant.conf 文件中读取控制接口目录。该文件通常存在于 /data/misc/wifi/ 中。使用 cat 命令打开此文件:

cat /data/misc/wifi/wpa_supplicant.conf

update_config=1
ctrl_interface=/data/misc/wpa_supplicant
eapol_version=1
ap_scan=1
fast_reauth=1

Note:to find wpa_supplicant.conf file you can search using find command in root directory. Goto root directory using cd / command and use find command to find wpa_supplicant.conf:

注意:要查找 wpa_supplicant.conf 文件,您可以在根目录中使用 find 命令进行搜索。使用 cd / 命令进入根目录并使用 find 命令找到 wpa_supplicant.conf:

find . -name wpa_supplicant.conf

找 。-name wpa_supplicant.conf

Go to control interface directory specified by ctrl_interafce. First file in this directory is the interface name.

转到由 ctrl_interafce 指定的控制接口目录。此目录中的第一个文件是接口名称

cd /data/misc/wpa_supplicant
ls
wlan0

You are going to need "control interface" and "interface name" for executing wpa_cli command.

您将需要“控制接口”和“接口名称”来执行 wpa_cli 命令。

Note:if you incorrectly input these 2 parameters for wpa_cli command, the wpa_cli could not connect to wpa_supplicant and returns this message:

注意:如果您为 wpa_cli 命令错误地输入了这 2 个参数,则 wpa_cli 无法连接到 wpa_supplicant 并返回此消息:

Interactive mode

Could not connect to wpa_supplicant: plan - re-trying

Or it may connect to wpa_supplicant but return UNKNOW COMMAND for its interactive commands like this:

或者它可能连接到 wpa_supplicant 但返回 UNKNOW COMMAND 为其交互式命令,如下所示:

> scan
UNKNOWN COMMAND
>add_network
UNKNOWN COMMAND

3- Execute wpa_cli command using above mentioned parameters:

3- 使用上述参数执行 wpa_cli 命令:

wpa_cli -p [control directory path] -i [interface name]
wpa_cli -p /data/misc/wpa_supplicant -i wlan0

This commands then enter to interactive mode where you can scan and find networks, attach to them and ...

此命令然后进入交互模式,您可以在其中扫描和查找网络,附加到它们并...

# Add new WiFi network
add_network
set_network 0 auth_alg OPEN
set_network 0 key_mgmt WPA-PSK
set_network 0 ssid "network_name"
set_network 0 proto RSN
set_network 0 mode 0
set_network 0 psk "password"

# Connect to it
select_network 0
enable_network 0
reassociate

# Check the status
status

save_config

Using save_config you can store these settings back into the wpa_supplicant.conf file for future use. You can recall these setting next time by enable_network command. Next time you want to enable wifi use these commands:

使用 save_config,您可以将这些设置存储回 wpa_supplicant.conf 文件以备将来使用。下次可以通过 enable_network 命令调用这些设置。下次要启用 wifi 时,请使用以下命令:

wpa_cli -p /data/misc/wpa_supplicant -i wlan0

enable network 0

0 is network id. You can use list_networks to find other stored configurations. For further information regarding wpa_cli refer to this document: Also full interactive commands of wpa_cli is documented in this page.

0 是网络 ID。您可以使用 list_networks 查找其他存储的配置。有关 wpa_cli 的更多信息,请参阅此文档:本页还记录了 wpa_cli 的完整交互式命令。

http://w1.fi/cgit/hostap/plain/wpa_supplicant/README

http://w1.fi/cgit/hostap/plain/wpa_supplicant/README

I used this procedure for configuring wifi on Android on Orange Pi 2G IOT.

我使用此过程在 Orange Pi 2G IOT 上的 Android 上配置 wifi。