[Samsung Galaxy Nexus] codword:
Hi All,

Yes, I'm a noob to xda-developers, but not a noob in general. Old-school linux nerd / C Programmer / Network engineer. Saddled with a Blackberry most of my career, I haven't ventured into the Android scene, until now. Just recently purchased a Galaxy Nexus.

I am increasingly frustrated with Google's android market, and how difficult it is to find apps that work on my phone, version of ICS, etc. After about a day of trying various apps, I gave up looking for an easy way to 'masquerade' behind my phone to use my phone's data plan (as opposed to paying extra for hotspot).

So, I wrote a little script to do it, and thought I would share it with you good people. I have interest in writing an app to do this at some point, but I have too many irons in the fire at the moment.

Run this script on your linux laptop/computer. It'll send the necessary commands to your phone.

Requirements:

1. Galaxy Nexus w/ ICS (I'm currently running 4.0.2) [ This may very well work on other Android platforms as well, but I don't have access to test ]

2. Root phone (was easy enough to do)

3. Install OpenVPN and busybox binaries (I used OpenVPN installer from the market, and I forget where I found busybox binaries, but it wasn't hard to find them)

4. You must have Android SDK installed on the linux box you want to tether. (Many distros have it readily available. For me, arch linux's AUR has it)

Sounds like a hassel if you're a noob, but these are things you'll eventually want to do anyway.

Lastly, just this script.

just run adbtether start or adbtether stop.

Good luck!

#!/bin/bash

# DNS information your tethered computer will use
DOMAIN="yourdomain.com"
DNS1="8.8.4.4"
DNS2="8.8.8.8"

function clean()
{
# Shutdown any current networking
sudo netcfg -a
killall -15 openvpn &>/dev/null

# Kill local iptables - Android will protect us :)
for table in filter nat mangle
do
sudo iptables -t $table -F
sudo iptables -t $table -X
done

sudo iptables -t filter -P OUTPUT ACCEPT
sudo iptables -t filter -P INPUT ACCEPT
sudo iptables -t filter -P FORWARD ACCEPT
}

function start()
{
# TCP Port forwarding over adb - in theory this would work over bluetooth too but I havent tried
adb forward tcp:6810 tcp:6810

# Setup Android's linux kernel for IP Forwarding and NAT
adb shell "su -c 'killall -15 openvpn;iptables -t nat -F POSTROUTING;iptables -t nat -A POSTROUTING -s 192.168.251.2/32 -j MASQUERADE;iptables -t filter -F FORWARD;iptables -t filter -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT;iptables -t filter -A FORWARD -s 192.168.251.2/32 -m state --state NEW -j ACCEPT;echo 1 >/proc/sys/net/ipv4/ip_forward'"

# Establish tunnel
adb shell "su -c 'openvpn --daemon --remote 127.0.0.1 --lport 6810 --proto tcp-server --dev tun --ifconfig 192.168.251.1 192.168.251.2'"
sudo openvpn --daemon --remote 127.0.0.1 6810 --proto tcp-client --dev tun --ifconfig 192.168.251.2 192.168.251.1 --route 0.0.0.0 0.0.0.0

# Setup local resolver
cat >/tmp/resolv.conf <<_EOF_
search $DOMAIN
nameserver $DNS1
nameserver $DNS2
_EOF_

sudo mv /tmp/resolv.conf /etc
sudo chown root.root /etc/resolv.conf
sudo chmod 0644 /etc/resolv.conf
echo Tethering enabled.
}

function stop()
{
sudo killall -15 openvpn
adb shell "su -c 'killall -15 openvpn;iptables -t nat -F POSTROUTING;iptables -t filter -F FORWARD;echo 0 >/proc/sys/net/ipv4/ip_forward'"
echo Tethering disabled.
}
if [[ $(adb get-state) != "device" ]]; then
echo Error with adb
exit 1
fi

clean

case $1 in
start)
clean
start
;;
stop)
clean
stop
;;
*)
echo 'Usage: adbtether < stop | start >'
;;
esac

exit 0