简体   繁体   中英

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:

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 .

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. The EXTRAS are then provided using the am command's -e <KEY> <VALUE> parameter.

I already build an app which does so and it's available here: 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 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.

Hope it helps someone.

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

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 . It happens because adb itself doesn't run in ROOT mode. 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:

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

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:

in the same command prompt box,

  • type adb root to 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/wifi click enter.
  • Lastly type cat wpa_supplicant.conf click enter.

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

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

-  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

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

To connect to an open wifi network 'Public' use

adb shell cmd -w wifi connect-network Public open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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