Wednesday, December 24, 2014

Android Tethered Connection through Ubuntu 14.04 Laptop setup as a Router to 10.x.x.x lan


So in order that all the micro-servers in the "Driveby" application could have internet access while I'm doing real drive-testing as well as the Laptop I'm using as the main System Interface to control everything and also access Google Maps to display mapped geo-tagged, RF Noise Levels I needed to setup the central Laptop to share it's Tethered internet connection on the Galaxy Note 2 and it's 3G/4G LTE.

I found this example and adapted it to suite my needs.


On the TK1 I used nm-connection-manager to setup the 10.0.1.x network connection.


On the Laptop I used the System > Networks GUI to setup the ETH0 10.0.1.x network connection.


Next on the LAPTOP I used detail found here to do this:

vi /etc/sysctl.conf
net.ipv4.ip_forward = 1

sysctl -p /etc/sysctl.conf

THEN this little bit of magic brings it all together.



When the Laptop is connected to the TETHERED connection:



root@driveby-lt:~# cat driveby_routing_usb
#!/bin/bash
sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
sudo iptables -A FORWARD -i usb0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o usb0 -j ACCEPT
route delete default
route add default gw 192.168.42.129

When the Laptop is connected to the WIFI connection:


root@driveby-lt:~# cat driveby_routing_wlan 
#!/bin/bash

sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

route delete default
route add default gw 192.168.3.1

So when I'm connected to the WIFI internet connection on the Laptop I simply run as root:

~/driveby_routing_wlan

and when on the Tethered connection:

~/driveby_routing_usb0

I will probably add these deeper into the networking configuration so that I don't have to actually run these scripts manually.  But this works Great!

From the TK (note the chart layout above) I can now access the Global Internet via whatever REAL internet connection the Laptop currently has available be-it WIFI or Tethered connection.

In this way everything being used in the Driveby Application will have real time access to the internet if needed.  Pretty cool!

And I didn't have to pay for adding Hotspot on my tethered device.  This setup allows me to add as many devices to the sub-network on 10.x.x.x as I need to and the Laptop acts like a Masquerading Router!

Excellent!

No comments:

Post a Comment