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.
Related
I want to connect with my linux pc through ssh to my linux device which is connected via USB to my android device(Only using adb shell commands)
For now I'm doing it using two apps which is installed on my android device:
1. Network IP scanner, which gives me the IP of the target device.
2. FwdPortForwardingApp (https://github.com/elixsr/FwdPortForwardingApp)
With that App I perform port forwarding using BOTH udp and tcp protocols from wlan0 on port 2222 to the target IP I got from the network IP scanner on port 22.
I want to do it using only adb shell commands, how can I do it?
Tried adb forward tcp:2222 tcp:22 and adb reverse tcp:2222 tcp:22 which didn't help
Expected to perform ssh using port 2222 directly to my device using only adb shell commands (Cause I want to create a script which will perform all the commands together without using the android device)
Greetings to everyone!
I have a project on RPI but I have 1 problem.
Project description: RPI (server, connected to LAN DHCP with internet access) + Android Tablet (connects on USB0 static interface on RPI via USB Tethering).
Android App: Made an android APP to connect to server and manage RPI as client via sockets and ajax requests.
Problem: I have made five examples of same thing with 5 tablets and 5 RPI-i and USB0 is being seen by LAN Router and the problem is that I want nobody from LAN (not even another tablet) can connect to USB0 but that tablet connected to RPI with USB Tethering. Now how 5 devices is connected to LAN sometimes happens that tablet from RPI1 connects via LAN to RPI2 beacuse USB0 can be seen on LAN.
I want that USB0 is not seen on LAN but can give internet to tablet via eth0.
Currently I'm giving USB0 internet like this:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE && sudo iptables -A FORWARD -i eth0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT && sudo iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT
I would really be glad if somebody could help me I'm not so good in configuring network interfaces
Check out this project. parts of it are probably quite useful for you.
https://github.com/samyk/poisontap/blob/master/pi_startup.sh
I have an android device connected through adb over wifi. Now, due to some reason, the adb server is killed using command
'adb kill-server'.
Once I restart the server or issue the command
'adb devices', I would like the devices that were connected over wifi to appear in the list of devices, Just like the devices connected by usb appear in the list.
How can this be achieved? Can I put the ipaddresses of the devices in some file and they get connected automatically when the adb server restarts?
I have made batch scripts for automatically setting up a device for Wifi adb bridge, getting the IP and connecting to it. You just plug your device in, run the script and then unplug the device again.
Windows batch (wifi-connect.bat):
#echo off
echo Disconnecting old connections...
adb disconnect
echo Setting up connected device
adb tcpip 5555
echo Waiting for device to initialize
timeout 3
FOR /F "tokens=2" %%G IN ('adb shell ip addr show wlan0 ^|find "inet "') DO set ipfull=%%G
FOR /F "tokens=1 delims=/" %%G in ("%ipfull%") DO set ip=%%G
echo Connecting to device with IP %ip%...
adb connect %ip%
pause
Unix / Mac (wifi-connect.sh)
#!/bin/sh
adb disconnect
adb tcpip 5555
sleep 3
IP=$(adb shell ip addr show wlan0 | grep 'inet ' | cut -d' ' -f6| cut -d/ -f1)
echo "${IP}"
adb connect $IP
Both scripts requires adb to be in your path or in the same folder as the script.
You cannot automatically connect your device over WiFi if it that DEVICE is not connected using a USB cable first, because you need to config the device to listen a port and open a connection. What you can do is try to run these commands using a script.
From a computer, if you have USB access already (NO root required)
1. For Linux and MAC User:
Step 1:
Open terminal and install adb using
sudo apt-get install android-tools-adb android-tools-fastboot
Step 2:
Connect your phone via USB cable to the PC. Type following command in terminal to get the device ID:
$ adb devices
List of devices attached
LGV498b9cacc1 device
192.168.1.187:5558 device
192.168.1.184:5557 device
192.168.1.186:5556 device
192.168.1.143:5555 device
Step 3:
Using the device name listed above, get the IP of your Android device (if you know you can skip this step)
$ adb -s LGV498b9cacc1 shell ip -f inet addr show wlan0
22: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
inet 192.168.1.185/24 brd 192.168.1.255 scope global wlan0
Step 4:
Setup the communication port using this command:
$ adb -s LGV498b9cacc1 tcpip 5559
restarting in TCP mode port: 5559
Step 5:
Connect to your Android device IP address.
$ adb -s LGV498b9cacc1 connect 192.168.1.185:5559
connected to 192.168.1.185:5559
Step 6:
Verify if the device was added to the list:
$ adb devices
List of devices attached
192.168.1.185:5559 device
LGV498b9cacc1 device
192.168.1.187:5558 device
192.168.1.184:5557 device
192.168.1.186:5556 device
192.168.1.143:5555 device
If you are using Windows operating system, you could create a batch file and add
adb connect 192.168.1.179
please replace 192.168.1.179 with your own device ip address.
Then save the bat file and put it in startup folder.
You can't automatically connect your device over wifi if it's not connected using a USB cable. I've developed an open source IntelliJ Plugin do this as fast as possible. Here you have de code https://github.com/pedrovgs/AndroidWiFiADB and here the plugin https://plugins.jetbrains.com/plugin/7983
No you can't do that because once connection to adb server is killed you have to make connection again . If you still want to connect automatically and don't have to write command again and again than You can create a batch/Script file with all the command and make it run at time of reconnecting to adb devices .
I went through this tutorial http://blog.mathieu.carbou.me/post/60454997009/reverse-usb-tethering-with-android-2-2 to reverse tether my android phone to use PC internet via USB.
Using following commands I am successfully able to create bridge br0.
sudo ifconfig eth0 0.0.0.0
sudo ifconfig usb0 0.0.0.0
sudo brctl addbr br0
sudo brctl addif br0 eth0
sudo brctl addif br0 usb0
sudo ifconfig br0 up
sudo dhclient br0
br0 has ip address : 172.16.138.102
My android phone is showing rndis0. I used following command using adb shell as root.
netcfg rndis0 dhcp
rndis0 was assigned ip : 172.16.138.109/24
Then I added default gateway for rndis0 interface.
route add default gw 172.16.138.102 dev rndis0
Still internet was not working on android. I tried several other tutorials but same info was mentioned everywhere. Am I missing something or I need to do something more for running internet?
Try to use Reverse Tethering tool.
Its in beta version so Android Browser will not know that Internet is working but you can surf internet.
Use gnirehtet.
Note: After getting connected to gnirehtet server, just keep the data connection ON, although your data will not be used, only your PC internet will be used on android.
I have a scenario where I need to start Android Emulator with a specific IP Address?
Can I start the emulator like that?
I do not want to do IP forwarding or other stuff, because there is an Android program running in the Emulator which on boot will configure itself with Android Emulators IP address present during boot time.
As by default the ip address of emulator is 10.0.2.15 which I need to change.
Also, this Ip is set in init.goldfish.rc and init.goldfish.sh.
If I change them, still the ip of android is 10.0.2.15.
I made work by this way:
In Android Emulator if you do ifconfig then you will see bridge, eth0 & eth1 as network devices. Where bridge has the 10.0.2.15 as ip and eth0 is up but without any ip and eth1 is down without any ip.
Now create a TAP and Bridge devices on you host machine and bridge your TAP device with any of the working ethernet cards on you host machine.
The TAP device (tap1) and bridging (br1) it with eth0 can be created following below steps:
$sudo ip tuntap add dev tap1 mode tap
$sudo ip link show dev tap1
$sudo brctl addbr br1
$sudo brctl addif br1 tap1
$sudo brctl addif br1 eth0
$sudo ip link set eth0 up
$sudo ip link set br1 up
$sudo brctl show
So now once your TAP is up and if your eth1 is connect to any dhcp server, start the dhcp server.
Once the dhcp server is started, run the emulator with below command:
$sudo ./emulator -avd <avd_name> -qemu -net nic,vlan=1 -net user,vlan=1,hostname=<hostname_you_want> -net nic,vlan=2,macaddr=<mac_id_of_eth1_of_android_emulator> -net tap,ifname=tap1,script=no,vlan=2
In your android emulator shell run below command:
$netcfg
You should be able to see eth1 down with ip 0.0.0.0, now run below command to bring up the eth1:
$netcfg eth1 dhcp
And voila!!! you have the eth1 with ip address assigned from dhcp server.
Let me know if it works!!!
Now if you have any program in android emulator opening port on external server ip address it would go through eth0 of host machine to the external server.