Enable / disable wifi via bash scripting on Android - android

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

Related

Connect more than one android devices without USB(using wifi) is it possible?

I follow following steps for the connecting my device and already go-through this link Adb over wireless without usb cable at all and Connecting two android devices using wifi still not connected and this onle also adb error: more than one device - android
I connect my device to my computer via USB cable.
Go to platform tool path.
Run adb tcpip 5555.
Disconnect my device (remove the USB cable).
I Go to the Settings -> About phone -> Status to view the IP address of my phone.
Run adb connect :5555.
I get this error on terminal:
EDIT:
You have to provide the device id as well to open adb over wifi when more than 1 device is connected.
adb -s <device ID> tcpip 5555
I got the asnwer from here:
https://stackoverflow.com/a/38348791/4428159
provide your phone ip as well:
adb: usage: adb connect <host>[:<port>]
host is the ip address of the mobile you want to connect.
You have to do this for all the devices you want to connect to.

How to disconnect from a wifi network through adb?

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

Reverse tethering of several Android devices via USB

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.

List adb devices on network

I have a few devices enabled for over the air debugging. Is it possible to list the adb devices on the network? Something similar or to the same effect as adb devices but for devices that are enabled for over the air debugging.
If you do these steps exactly and run the adb devices command, the android device should appear under the List of Attached Devices. First open a command window and make sure you are either in the same directory as adb or have adb in your PATH variable. Then execute the following list of commands:
$adb usb
restarting in USB mode
$ adb devices
List of devices attached
######## device
$ adb tcpip 5555
restarting in TCP mode port: 5555
Get the IP address of your android device. (Usually under System settings then Network settings, you can look up how to get the IP address on your specific device). The IP address should look something like 12.34.56.78 (this could vary though). Once you have the IP address continue with the following commands:
$ adb connect 12.34.56.78
connected to 12.34.56.78:5555
Remove the USB cable from the device
$ adb devices
List of devices attached
12.34.56.78:5555 device
Source
That's not possible with the Android SDK, as it would involve a huge network scan without any clue, just blind-pinging port 5555 of every possible address on the network.
But probably you can do it with a network scanner that looks for port 5555 open.

Simulate Wireless Network in Emulator

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.

Categories

Resources