Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 547
Grayson Henry Avatar asked Apr 03 '14 00:04

Grayson Henry


2 Answers

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.

like image 185
Andrejs Cainikovs Avatar answered Oct 07 '22 05:10

Andrejs Cainikovs


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.

like image 1
MDR Avatar answered Oct 07 '22 06:10

MDR