Skip to content

Instantly share code, notes, and snippets.

@shouya
Last active September 7, 2024 05:47
Show Gist options
  • Save shouya/3965214 to your computer and use it in GitHub Desktop.
Save shouya/3965214 to your computer and use it in GitHub Desktop.
Share internet connection to android phone
#!/bin/sh
[ $UID = "0" ] || {
echo "Please run as root."
exit
}
[ $1 == '--help' ] && {
echo <<HERE
To use this file, set 'USB Tether' on the phone first
HERE
exit
}
my_ip="192.168.42.99"
my_interface="usb0"
my_output_interface="wlan0"
phone_ip="192.168.42.98"
phone_interface="usb0"
dns1="8.8.8.8"
dns2="8.8.4.4"
echo Turning interface on.
ifconfig $my_interface $my_ip up 2>/dev/null
echo Setting remote ip address.
adb shell ifconfig $phone_interface $phone_ip up 2>/dev/null
echo Removing old routing rules \(sharing\) on phone.
conn=`adb shell busybox ip route | grep $phone_interface | sed 's/ .*$//'`
echo conn | xargs -I9 adb shell busybox ip route del 9 2>/dev/null
echo Removing default rule on phone.
adb shell busybox ip route del default
echo Setting local route rules.
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
ip ro | grep $my_interface | sed 's/ .*$//' | xargs -I9 ip ro del 9
ip ro add $phone_ip dev $my_interface
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -j MASQUERADE -A POSTROUTING -o $my_output_interface
echo Setting remove route rules.
adb shell busybox ip ro add default dev $phone_interface
adb shell busybox ip ro change default via $my_ip
echo Setting DNS server on phone.
adb shell setprop net.dns1 $dns1 2>/dev/null
adb shell setprop net.dns2 $dns2 2>/dev/null
echo Now I\'ll try to ping google, read the result.
adb shell busybox ping www.google.com 2>/dev/null
echo Done if passed the ping test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment