Skip to content

Instantly share code, notes, and snippets.

@0xItx
Forked from mrenouf/reverse-tether.sh
Last active September 10, 2015 07:51
Show Gist options
  • Save 0xItx/9e6b64e5293e87c874ab to your computer and use it in GitHub Desktop.
Save 0xItx/9e6b64e5293e87c874ab to your computer and use it in GitHub Desktop.
USB reverse tethering script (tested with Ubuntu 15.04 & Samasung Galaxy)
#!/bin/bash
set -x
ADB="$(which adb)"
# We need root on the host to mess with networking
if [[ $(whoami) != "root" ]]; then
echo "You must be root to run this script!"
exit 1
fi;
# We need root on the device to mess with networking
$ADB -d root
# Keep NetworkManager from messing with the adapter
grep 'usb0 inet manual' /etc/network/interfaces
if [ ! $? ]; then
echo 'Configuring usb0 for manual control'
echo 'iface usb0 inet manual' >> /etc/network/interfaces
systemctl restart network-manager
fi
# Turn on usb networking, without dnsmasq or dhcp
echo 'Enabling usb network interface on the device'
$ADB -d shell setprop sys.usb.config rndis,adb
echo 'Waiting for adb to restart on the device...'
$ADB -d wait-for-device
echo 'Setting up usb networking on device'
$ADB -d shell 'ip addr add 192.168.200.2/30 dev rndis0;\
ip link set rndis0 up; \
ip route delete default; \
ip route add default via 192.168.200.1; \
setprop net.dns1 192.168.200.1'
echo 'Setting up usb interface on the host'
ifconfig usb0 192.168.200.1 netmask 255.255.255.252
ifconfig usb0 up
# Turn off the firewall if one is active
echo 'Checking for ufw firewall'
which ufw && ufw status || ufw disable
echo 'Enabling NAT and IP Forwarding'
# Start forwarding and nat (use existing default gw)
iptables -F -t nat
iptables -A POSTROUTING -t nat -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 'Starting dnsmasq'
killall dnsmasq
dnsmasq --interface=usb0 --no-dhcp-interface=usb0
echo 'Connection is active! Press any key to shutdown.'
read
echo 'Attempting to shut down reverse tethering'
killall dnsmasq
ip link set usb0 down
iptables -F -t nat
echo 0 > /proc/sys/net/ipv4/ip_forward
echo 'Disabling usb networking on host'
$ADB -d shell setprop sys.usb.config adb
$ADB wait-for-device
$ADB shell ip route delete default
echo 'Disable and re-enable Wifi to return the device to normal'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment