简体   繁体   English

使用 adb shell 连接到 WiFi

[英]Connecting to WiFi using adb shell

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.我只需要使用那个接入点,所以我需要的只是执行它的命令。

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

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 EXTRAS given when starting the app.基本上我的建议是创建一个应用程序,该应用程序根据启动应用程序时给出的EXTRAS加入接入点。 The EXTRAS are then provided using the am command's -e <KEY> <VALUE> parameter.然后使用am命令的-e <KEY> <VALUE>参数提供EXTRAS

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 ADB command:安装应用程序后,可以使用以下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.希望它可以帮助某人。

As an add-on: you can enable wifi via svc as root on the device作为附加组件:您可以在设备上以 root 身份通过svc启用 wifi

svc wifi enable

and disable via并禁用通过

svc wifi disable

As an another add-on: although my device was rooted I got remote object ''/data/misc/wifi/wpa_supplicant.conf'' does not exist error while trying to execute adb pull .作为另一个附加组件:虽然我的设备已植根,但我在尝试执行adb pull得到了remote object ''/data/misc/wifi/wpa_supplicant.conf'' does not exist错误。 It happens because adb itself doesn't run in ROOT mode.发生这种情况是因为adb本身不在 ROOT 模式下运行。 To work this around you can execute something like this要解决此问题,您可以执行这样的操作

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'"

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/wifi folder, reboot your device.Please note, different device has different content above the network line,don't modify that part.然后rm原始文件,添加push到/data/misc/wifi文件夹,重启你的设备。请注意,不同的设备在网络线以上的内容是不同的,不要修改那部分。

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 root to restart adb as root.输入adb rootadb root身份重新启动 adb。 click enter.点击进入。
  • Now type adb shell , click enter.现在输入adb shell ,点击回车。 makes sure the prompt shows root@[device]:确保提示显示root@[device]:
  • At the # prompt type cd /data/misc/wifi click enter.在 # 提示符下输入cd /data/misc/wifi点击回车。
  • Lastly type cat wpa_supplicant.conf click 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.在遇到“远程对象不存在”问题后,这些命令在我的无根设备上运行。

You can use the command adb shell cmd -w wifi connect-network with these parameters您可以使用带有这些参数的命令 adb shell cmd -w wifi connect-network

-  connect-network  open|owe|wpa2|wpa3 [] [-m] [-d] [-b ] [-r auto|none|persistent|non_persistent]
    Connect to a network with provided params and add to saved networks list
    open|owe|wpa2|wpa3 - Security type of the network.
     - SSID of the network
        - Use 'open' or 'owe' for networks with no passphrase
           - 'open' - Open networks (Most prevalent)
           - 'owe' - Enhanced open networks
        - Use 'wpa2' or 'wpa3' for networks with passphrase
           - 'wpa2' - WPA-2 PSK networks (Most prevalent)
    -m - Mark the network metered.
           - 'wpa3' - WPA-3 PSK networks
    -d - Mark the network autojoin disabled.
    -h - Mark the network hidden.
    -p - Mark the network private (not shared).
    -b  - Set specific BSSID.
    -r auto|none|persistent|non_persistent - MAC randomization scheme for the network

To connect to a wifi network 'Home' with a wpa2 authentication and passphrase as 'qwertyuiop' use要使用 wpa2 身份验证和密码短语“qwertyuiop”连接到 wifi 网络“Home”,请使用

adb shell cmd -w wifi connect-network Home wpa2 qwertyuiop

To connect to an open wifi network 'Public' use要连接到开放的 wifi 网络“公共”,请使用

adb shell cmd -w wifi connect-network Public open

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM