Skip to content

Instantly share code, notes, and snippets.

@bluec0re
Created July 24, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluec0re/234c7a18b521e7168b96 to your computer and use it in GitHub Desktop.
Save bluec0re/234c7a18b521e7168b96 to your computer and use it in GitHub Desktop.
#!/bin/bash
# enables reverse tether on android phones
# requires networkmanager
# tested & developed with CM12 (Android 5.1.1)
ADB=adb
# get by nmcli list
NM_CONN="048202e0-a62c-431b-8fc1-cf8fd552343e"
# the rndis device on your phone (adb shell ip link)
IF=usb0
WLANIF=wlan0
adb_root() {
$ADB shell "su -c '$1'"
}
adb_shell() {
$ADB shell "$1"
}
adb_wait() {
$ADB wait-for-device
}
info() {
echo -e "[\e[94m*\e[0m] $1"
}
warn() {
echo -e "[\e[93m~\e[0m] $1"
}
error() {
echo -e "[\e[91m!\e[0m] $1"
}
fatal() {
error "$1"
exit 1
}
success() {
echo -e "[\e[92m+\e[0m] $1"
}
setup_device_net() {
info "Enabled usb net"
adb_root 'setprop sys.usb.config rndis,adb'
adb_wait
success "Device back up"
adb_root "ip addr add 10.42.0.2/24 dev $IF"
adb_root "ip link set $IF up"
adb_root "ip route add default via 10.42.0.1"
}
setup_host_net() {
info "Starting connection"
(nmcli con up $NM_CONN && success "connected") || fatal "Couldn't start connection"
info "Setup iptables"
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1
success "DONE"
}
fix_routes() {
adb_root "ip route delete default table $1"
adb_root "ip route add 10.42.0.0/24 dev $IF table $1"
adb_root "ip route add default via 10.42.0.1 table $1"
}
setup_device_net
setup_host_net
# android maintains seperate routing tables for the devices
# -> set wlan table to route over $IF
fix_routes "$WLANIF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment