Connect to password protected wifi network using adb shell
I have an Android phone, and the goal is to connect the phone to a password protected wifi network. 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. Any help? I would rather not download programs onto the device
IF you really have root, you probably can insert a new record into whatever database of access points it has, but it will be tricky. Can you configure the database manually, and only activate it with ADB?
There is no such thing like a database of access points. Android keeps it’s list of configured AP’s in /data/misc/wifi/wpa_supplicant.conf file (might be different for different OEM’s and even phone models). Even though the easiest way would be to change this file by adding a new network block, the recommended approach is to use wpa_cli, wpa_supplicant command line interface. Check my answer.
2 Answers 2
This is possible to achieve using wpa_cli , command line interface of 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_network command will output the index of the new network, which should be used for the subsequent commands. In this example, this index is 0 .
@Cu635 There’s a high chance it is located in a directory which is not in your PATH. I’d advise you to search for wpa_cli binary in /vendor and other directories. Of course, there’s a chance OEM has removed wpa_cli binary from user builds, but highly unlikely. Also, there’s basically no alternative to wpa_supplicant (except iw , but it’s new, and I’m sure no OEM would make a device in 2017 with iw instead of wpa_supplicant ).
Use this procedure [more details included 🙂 ]
1- Make sure wpa_supplicant is running. Look for its pid using this command:
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:
svc wifi disable svc wifi enable
2- Read control interface directory from wpa_supplicant.conf file. This file usually exists in /data/misc/wifi/. Open this file using cat command:
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:
find . -name wpa_supplicant.conf
Go to control interface directory specified by ctrl_interafce. First file in this directory is the interface name.
cd /data/misc/wpa_supplicant ls wlan0
You are going to need «control interface» and «interface name» for executing wpa_cli command.
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:
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:
> scan UNKNOWN COMMAND >add_network UNKNOWN COMMAND
3- Execute wpa_cli command using above mentioned parameters:
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:
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.
I used this procedure for configuring wifi on Android on Orange Pi 2G IOT.
how to connect and disconnect from WiFi using ADB (not a root user)?
I found this project which allows me to connect to a WiFi through ADB commands not being a root user. I was wondering if anyone knows how to disconnect also from ADB and not being a root user. UPDATE: I’ve been recently trying:
C:\adb>adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings adb server is out of date. killing. * daemon started successfully * Starting: Intent < act=android.intent.action.MAIN cmp=com.android.settings/.wifi.WifiSettings >Warning: Activity not started, its current task has been brought to the front
C:\adb>adb -s serial_number shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings Starting: Intent < act=android.intent.action.MAIN cmp=com.android.settings/.wifi.WifiSettings >Warning: Activity not started, its current task has been brought to the front
adb shell input keyevent 20 & adb shell input keyevent 23
I can navigate and click. The problem seems to be that I always finish in a different state so the next time I input the command
adb -s serial_number shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings
I start in a different place and start clicking elsewhere. I’ve tried returning home, but that doesn’t fix the issue. Is there any way to start always from the same point inside the WiFi settings? Thanks!
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.
I have to automate a few things which only work with wi-fi.I can use monkey and click on settings->etc., but last thing would be how do i choose the network ABC as there may be more than one networks available. so if i could get a command, i can do that using SSID and password right away 🙂
I updated the wpa_supplicant.conf file with new SSID and password, enabled the wifi but this doesn’t seem to work on Nexus 4, Lolipop build. Wifi turns on but goes back to original SSID, that I was using via UI. I also noticed that my updated file was over written with the earlier SSID. I have change chown to system.wifi . Am I doing something incorrect here? I saw some post talking about sqlite3. Do we need to change anything in db?
7 Answers 7
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 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.
@Suban Dhyako It should tell you in your router settings what type of password/security it’s using. Also, in the android wifi screen, if you tap on a network it will tell you the security type. Most routers these days will be set up as WPA2 or WPA, and it seems that typing WPA in the adb command will work for either WPA or WPA2. Also, as a note to the author, this worked wonderfully, thank you. But the multi-line command formatting on the github didn’t work for me, while the single line command here did.
@gamingexpert13 thanks, glad to hear it worked for you. The multiline is probably only working on Linux 🙂
hey man I am trying to use your app but when I run the command it says «This application is meant to be used with ADB» what am I doing wrong?
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)"