Linux 使用 adb shell 连接到 WiFi

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8708886/
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-06 03:53:26  来源:igfitidea点击:

Connecting to WiFi using adb shell

androidlinuxadb

提问by Rohan

I have all the details to connect to a particular access point. I have to use that access point only, so all I require is the command to do it.

我拥有连接到特定接入点的所有详细信息。我只需要使用那个接入点,所以我需要的只是执行它的命令。

采纳答案by Bryan Buckley

You can add a network entry into the wpa_supplicant.conf yourself (or within your script) Essentially connect manually once, then do:

您可以自己(或在您的脚本中)将网络条目添加到 wpa_supplicant.conf 基本上手动连接一次,然后执行:

adb pull /data/misc/wifi/wpa_supplicant.conf

and integrate the network entry into your script for automation. Example simple script:

并将网络入口集成到您的脚本中以实现自动化。示例简单脚本:

#!/bin/bash
#

# Get this information by connecting manually once, and do
#   adb pull /data/misc/wifi/wpa_supplicant.conf
ADB_PULL="adb pull /data/misc/wifi/wpa_supplicant.conf"
WIRELESS_CTRL_INTERFACE=wlan0
WIRELESS_SSID=Gondolin
WIRELESS_KEY_MGMT="WPA-EAP IEEE8021X"
WIRELESS_EAP=PEAP
WIRELESS_USER=Turgon
WIRELESS_PASSWORD=IdrilCelebrindal

adb start-server
adb wait-for-device
echo "adb connection....[CONNECTED]"
adb root
adb wait-for-device
adb remount
adb wait-for-device

pushd /tmp
rm wpa_supplicant.conf 2>/dev/null # Remove any old one
adbpull_status=`$ADB_PULL 2>&1`
echo -e "\nAttempting: $ADB_PULL"
if [ `echo $adbpull_status | grep -wc "does not exist"` -gt 0 ]; then
    echo "  wpa_supplicant.conf does not exist yet on your device yet."
    echo "This means you have not used your wireless yet."
    echo ""
    echo "Taking our best shot at creating this file with default config.."

    echo "ctrl_interface=$WIRELESS_CTRL_INTERFACE" >> wpa_supplicant.conf
    echo "update_config=1" >> wpa_supplicant.conf
    echo "device_type=0-00000000-0" >> wpa_supplicant.conf
else
    echo $adbpull_status
    echo "  wpa_supplicant.conf exists!"
fi

echo ""
echo "Add network entry for wpa_supplicant.conf.."
echo "" >> wpa_supplicant.conf
echo "network={" >> wpa_supplicant.conf
echo "  ssid=\"$WIRELESS_SSID\"" >> wpa_supplicant.conf
echo "  key_mgmt=$WIRELESS_KEY_MGMT" >> wpa_supplicant.conf
echo "  eap=$WIRELESS_EAP" >> wpa_supplicant.conf
echo "  identity=\"$WIRELESS_USER\"" >> wpa_supplicant.conf
echo "  password=\"$WIRELESS_PASSWORD\"" >> wpa_supplicant.conf
echo "  priority=1" >> wpa_supplicant.conf
echo "}" >> wpa_supplicant.conf
echo "Pushing wpa_supplicant.conf.."
adb push wpa_supplicant.conf /data/misc/wifi/wpa_supplicant.conf
popd #/tmp

adb shell chown system.wifi /data/misc/wifi/wpa_supplicant.conf
adb shell chmod 660 /data/misc/wifi/wpa_supplicant.conf

echo ""
echo "Finished!"
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings
echo "Please toggle wifi off/on now.. (ifconfig not sufficient, monkey this)"

回答by serv-inc

As an add-on: you can enable wifi via svcas root on the device

作为附加组件:您可以在设备上以 root 身份通过svc启用 wifi

svc wifi enable

and disable via

并禁用通过

svc wifi disable

回答by Maxim Sultakov

As an another add-on: although my device was rooted I got remote object ''/data/misc/wifi/wpa_supplicant.conf'' does not existerror while trying to execute adb pull. It happens because adbitself doesn't run in ROOT mode. To work this around you can execute something like this

作为另一个附加组件:虽然我的设备已植根,但我 remote object ''/data/misc/wifi/wpa_supplicant.conf'' does not exist在尝试执行adb pull. 发生这种情况是因为adb它本身不在 ROOT 模式下运行。要解决此问题,您可以执行这样的操作

adb shell "su -c 'cp -R /data/misc/wifi/wpa_supplicant.conf /data/misc/wpa_supplicant.conf'"
adb shell "su -c 'chmod -R 777 /data/misc/wpa_supplicant.conf'"
adb pull /data/misc/wpa_supplicant.conf
adb shell "su -c 'rm /data/misc/wpa_supplicant.conf'"

回答by jpihl

Late to the party, but I came up with a way to accomplish this on a device without root.

聚会迟到了,但我想出了一种在没有 root的设备上完成此任务的方法。

It may not be pretty, but it works.

它可能不漂亮,但它有效。

Basically what I propose is to create an app that joins an access point based on EXTRASgiven when starting the app. The EXTRASare then provided using the amcommand's -e <KEY> <VALUE>parameter.

基本上,我建议创建一个应用程序,该应用程序根据EXTRAS启动应用程序时的给定加入接入点。在EXTRAS随后的使用所提供的am命令的-e <KEY> <VALUE>参数。

I already build an app which does so and it's available here: https://github.com/steinwurf/adb-join-wifi

我已经构建了一个应用程序,它可以在这里找到:https: //github.com/steinwurf/adb-join-wifi

Once the app is installed, a wifi access point can be joined using the following ADBcommand:

安装应用程序后,可以使用以下ADB命令加入 wifi 接入点:

adb shell am start -n com.steinwurf.adbjoinwifi/com.steinwurf.adbjoinwifi.MainActivity -e ssid [SSID] -e password_type [PASSWORD_TYPE] -e password [WIFI PASSWORD]

There's more info in the README on Github.

Github 上的 README 中有更多信息。

Hope it helps someone.

希望它可以帮助某人。

回答by yanzi1225627

I solve the problem by this: adb pull /data/misc/wifi/wpa_supplicant.conf ~/Desktop,and then edit the file, add network module, my whole conf file is:

我通过这个解决了这个问题: adb pull /data/misc/wifi/wpa_supplicant.conf ~/Desktop,然后编辑文件,添加网络模块,我的整个 conf 文件是:

##### wpa_supplicant configuration file template #####
update_config=1
ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=wifi
eapol_version=1
ap_scan=1
fast_reauth=1
network={
    ssid="your ssid"
    psk="your pswd"
    key_mgmt=WPA-PSK
    priority=241
}

Then rm the origin file, add push it to /data/misc/wififolder, reboot your device.Please note, different device has different content above the network line,don't modify that part.

然后rm原始文件,添加push到/data/misc/wifi文件夹,重启设备。请注意,不同的设备在网络线以上的内容是不同的,不要修改那部分。

回答by sarah

super late but i hope this'll help anybody who may stumble upon this thread.

太晚了,但我希望这会帮助任何可能偶然发现此线程的人。

if you're trying the adb pull method but received "remote object does not exist", try this:

如果您正在尝试 adb pull 方法但收到“远程对象不存在”,请尝试以下操作:

in the same command prompt box,

在同一个命令提示符框中,

  • type adb rootto restart adb as root. click enter.
  • Now type adb shell, click enter. makes sure the prompt shows root@[device]:
  • At the # prompt type cd /data/misc/wificlick enter.
  • Lastly type cat wpa_supplicant.confclick enter.
  • 键入adb root以 root 身份重新启动 adb。点击进入。
  • 现在输入adb shell,点击回车。确保提示显示root@[device]:
  • 在 # 提示符下键入,cd /data/misc/wifi单击 Enter。
  • 最后输入cat wpa_supplicant.conf点击回车。

this should dump data of WiFi you've previously connected to on your phone, to your pc screen.

这应该将您之前在手机上连接的 WiFi 数据转储到您的电脑屏幕。

these commands worked on my unrooted device after running into the “remote object does not exist” issue.

在遇到“远程对象不存在”问题后,这些命令在我的无根设备上运行。