Connect to password protected wifi network using adb shell
Asked Answered
V

2

19

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

Vevina answered 3/4, 2014 at 0:8 Comment(2)
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?Thegn
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.Nail
N
25

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.

Nail answered 7/4, 2014 at 13:55 Comment(4)
Thanks that helped. It looks like you got psk and ssid the wrong way around.Loran
Are the options to wpa_cli required? On my phone I did not have to use any options to add a network.De
Hi, now in Android 8, wpa_cli command is not present, is there any alternatives?Verleneverlie
@Verleneverlie 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).Nail
I
1

Use this procedure [more details included :) ]

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

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:

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.

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

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

Insurrectionary answered 11/4, 2018 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.