Android device: How to connect wifi by "Adb shell cmd wifi" command?
I tried to connect wifi by adb to avoid tedious click and verification operations by appium. And i found that the "adb shell cmd wifi" have a keyword "connect-network", but i cant connect the wifi by this way. Does anyone know how to use it? Thank a lot guys!
Here is the usage of the "Adb shell cmd wifi connect-network":Wi-Fi (wifi) commands:
connect-network <ssid> open|owe|wpa2|wpa3 [<passphrase>] [-m] [-d] [-b <bssid>]
Connect to a network with provided params and add to saved networks list
<ssid> - SSID of the network
open|owe|wpa2|wpa3 - Security type 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)
- 'wpa3' - WPA-3 PSK networks
-m - Mark the network metered.
-d - Mark the network autojoin disabled.
-b <bssid> - Set specific BSSID.
And my input is adb shell cmd wifi connect-network WIFIName open wpa2 Loginpassword, it return me a Connection initiated, But the connect is not successful. Why this happen? Should i input the wifi login account? And what the whole command should be?
Hey I think that you should use either 'open' or 'wpa2' not both. But If we assume that wifi is secured by a wpa2 password so proper command would be:
adb shell cmd wifi connect-network WIFIName wpa2 password
Also, take into consideration that only the root user can invoke wifi commands, and root access is not available in most cases.
Related
How can I remove the saved wi-fi network from my rooted android device?
I have tried copying the custom wpa_supplicant.conf file to /data/misc/wifi/
>adb push wpa_supplicant.conf /data/misc/wifi/wpa_supplicant.conf
The file copied successfully but it won't remove the saved wi-fi network.
I have following content in the custom wpa_supplicant.conf
ctrl_interface=/data/misc/wifi/sockets
disable_scan_offload=1
driver_param=use_p2p_group_interface=1p2p_device=1use_multi_chan_concurrent=1
update_config=1
device_name=Tablet
manufacturer=MSI
model_name=550-18900(MiniTouch)
model_number=550-18900(MiniTouch)
serial_number=0123456789ABCDEF
device_type=10-0050F204-5
config_methods=physical_display virtual_push_button
pmf=1
external_sim=1
I also tried removing wpa_supplicant.conf file.
I also tried turning off wifi and then pushing custom wpa_supplicant.conf file. I am not able to a removed the saved wifi network.
I found the alternative way to remove the saved network from rooted device. This can be achieved using wpa_cli command.
$adb shell wpa_cli list_networks
//this will show the list like following
network id / ssid / bssid / flags
0 worldink_wifi any [CURRENT]
//now you can remove the network using its network id
Then you can remove network using
$adb shell wpa_cli remove_network 0
//0 is network id
Install adb, and connect your device and execute the following command :
adb shell am start -n 'com.adbwifisettingsmanager/.WifiSettingsManagerActivity' --esn remove -e ssid <ssid_name>
Replace the <ssid_name> with the name of the wifi network.
I understand we could turn off Wifi through the "adb shell svc wifi disable" command but I don't want to completely turn off Wifi. I want to disconnect from a particular ssid through adb. Is it possible?
Edit: I got a notification saying this question is identified as a dupe of this question: How to turn off Wifi via ADB?. It actually is not. As mentioned clearly in the first paragraph, I don't want to turn off wifi but want to simply disconnect from a particular network. This is like long pressing a network and tapping "Forget network". Essentially I want to simulate a condition of the user moving out of a wifi network without having to turn off wifi.
If you have root access, you can make it by using wpa_cli.
First, you can use the wpa_cli`s list_networks to get the network id of the network that you want to disconnect
$ adb shell
# wpa_cli
> list_networks
**network id** / ssid / bssid / flags
0 001aLinksys14081-2G any [CURRENT]
and then you just need to run the wpa_cli remove_network {network id}, where the {network id} parameter is the one that you got on the list_networks method.
One option is to use cmd wifi. For disconnecting from a Wi-Fi network, you can "forget" it like so:
adb shell cmd wifi list-networks
adb shell cmd wifi forget-network <networkId from list-networks>
You can view the help documentation for cmd wifi with:
adb shell cmd wifi -h
This command did it for me
docker exec -it $container_id /bin/bash -c "cd /root//shared-tools/android-sdk/platform-tools; ./adb shell 'svc wifi disable'"
You cannot. But, this will not be enough to satisfying your question, there is tricky way to connect to "other wifi". (Yes, it is not disconnect)
Install an app, and send command to the app via adb.
See this: https://stackoverflow.com/a/37303412/850347
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]
See this: https://stackoverflow.com/a/37303412/850347
I'm trying to setup a testing environment for my android app where the separate devices need an Internet connection, but don't have wireless capabilities.
So I decided on using usb reverse tethering. My host machine is running Ubuntu. I already managed to setup reverse tethering for one device, the connection is working and I can ping google.com from this device's adb shell.
When I connect a second device, the first tethering stops working.
Both devices have the same IP adress configured on their internal usb0 interface.
Here's what I have so far:
Port forwarding on host is activated
echo 1 > /proc/sys/net/ipv4/ip_forward
Iptables rule on host to route traffic:
iptables -t nat -A POSTROUTING -s 169.254.255.2/24 -j MASQUERADE
Plugin first device:
ifconfig shows the device on usb0 # ip 169.254.255.2/30
On first device (with adb shell)
> route add default gw 169.254.255.2 dev usb0
> setprop net.dns1 8.8.8.8
> ping google.com --> is giving results
Plugin second device
ifconfig shows this device on usb1 # ip 169.254.255.2/30
On second device
Add gw, set DNS like above
Now the second device can ping google.com, but the first one not.
When I change the internal IP or the IPs assigned to the hosts usb0, usb1 interfaces I am not able to connect to the devices via adb anymore.
I suspect this to be a networking problem, rather than a problem with ADB? Any Ideas?
After a lot of googling and trial and error I finally found the solution for my particular setup.
What I did in the end, and what I missed in my first attempts, was to put each connected usb device into a separate subnet (see this answer on serverfault).
To put it clearly, the setup is now like following:
iptables contains the following rule
iptables -t nat -A POSTROUTING -j MASQUERADE
the host's usb0 and usb1 interface, now in separate subnets
usb0: inet addr:169.254.0.1 Bcast:169.254.15.255 Mask:255.255.240.0
usb1: inet addr:169.254.16.1 Bcast:169.254.31.255 Mask:255.255.240.0
internal usb0 iface of Android device connected on host's usb0:
- usb0 169.254.0.2/20
- default gw 169.254.0.1 (so the host's usb0 iface)
internal usb0 iface of Android device connected on host's usb1:
- usb0 169.254.16.2/20
- default gw 169.254.16.1 (so the host's usb1 iface)
That way I now have Internet access (after adb shell setprop net.dns1 8.8.8.8) from each android device connected to the usb interfaces on my host.
You need a linux system with root access, rooted android phone and usb cable.
Steps
Install adb and fastboot in your linux system.
sudo apt-get install android-tools-adb android-tools-fastboot
Connect your android phone to your PC via USB and enable USB tethering from the settings on your phone.
Open terminal in your PC and type the commands given below.
Find out the new network interface that was created on your PC.
ifconfig
Generally the new network interface will be usb0 or usb1. Let us consider usb0.
Now make some static IP address for this new interface usb0.
sudo ifconfig usb0 10.42.0.1 netmask 255.255.255.0
Now make this new interface usb0 as shared connection.
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Now make some changes iptables firewall.
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
Now we have to enter some commands in the android phone. We can use either adb or directly typing the commands in the android phone through a terminal emulator installed in the phone. I am using the adb way.
Find the network interface in the android phone.
adb shell busybox ifconfig
Mine was rndis0.
Set the static IP, netmask and gateway.
adb shell ifconfig rndis0 10.42.0.2 netmask 255.255.255.0
adb shell route add default gw 10.42.0.1 dev rndis0
Verify Internet connectivity on your phone.
adb shell ping 8.8.8.8
If it is pinging, then the Internet in your mobile is working.
This Internet connection will not work with the apps like opera mini and paytm, but works well with whatsapp.
I am trying to enable / disable the wifi device in my android device in a bash script. I am using the terminal emulator and the program Script Manager to execute bash scripts on the phone (which is a rooted Nexus One).
The normal way to do this in linux would be something like
ifconfig eth0 up
That gives me
"eth0: no such device"
Trying
iwconfig eth0
gives the same, and
iwconfig
gives a list of devices with no eth0 on it. Now if I turn on wifi (manually from the GUI) and type
iwconfig
it shows eth0 with a lot of info.
My question is: How can I use the command line to turn on / off wifi? And why is eth0 disappearing from the device list when wifi is disabled?
thanks.
While it doesn't work in my non-rooted phone for obvious reasons, the results I'm obtaining are completely different while wifi is connected:
$ ifconfig eth0
eth0: ip 192.168.0.102 mask 255.255.255.0 flags [up broadcast running multicast]
$ ifconfig eth0 up
error: SIOCSIFFLAGS (Permission denied)
If you just want to turn wifi on or off you should use:
# svc wifi enable
or
# svc wifi disable
to see the help message
# svc wifi
Control the Wi-Fi manager
usage: svc wifi [enable|disable]
Turn Wi-Fi on or off.
svc wifi prefer
Set Wi-Fi as the preferred data network
How do I simulate a WiFi network using the Emulator?
Simple answer: you can't. Also, this has been asked before (and yes, it's still valid).
The emulator does not emulate WiFi specifically. It emulates a generic network connection. You can use telnet commands to configure its characteristics (e.g., speed, latency).
Here is the solution I came up with for simulating total network connection loss on the emulator:
Write the following script and name it "nonetwork.sh"
netcfg eth0 down
sleep 10
netcfg eth0 up
netcfg eth0 dhcp
Upload the following script to the emulator via this command:
adb push nonetwork.sh /data/local/nonetwork.sh
Change the permissions
adb shell chmod 700 /data/local/nonetwork.sh
Run it
adb shell /data/local/nonetwork.sh
While the network is down on the device you'll lose access to adb as well but once the connection is re-established it will work again. Hope this helps.