Skip to content

Instantly share code, notes, and snippets.

@djeraseit
Forked from poqdavid/setip
Created September 7, 2024 23:43
Show Gist options
  • Save djeraseit/8828f1f299f18f7dbc7ecd6603f71d44 to your computer and use it in GitHub Desktop.
Save djeraseit/8828f1f299f18f7dbc7ecd6603f71d44 to your computer and use it in GitHub Desktop.
Sets static IP for Android 11 hotspot
#!/system/bin/sh
NEW_SUBNET='192.168.43'
WIFI_INTERFACE='wlan0'
WIFI_INTERFACE_INFO=$(ip addr show dev ${WIFI_INTERFACE} | grep -m1 "$WIFI_INTERFACE:")
if [[ "$WIFI_INTERFACE_INFO" == *"state UP"* ]]; then
LOCAL_TABLE=$(awk '$2=="local_network" {print $1}' /data/misc/net/rt_tables)
ip route replace ${NEW_SUBNET}.0/24 dev ${WIFI_INTERFACE} table $LOCAL_TABLE
ip address replace ${NEW_SUBNET}.1/24 brd + dev ${WIFI_INTERFACE}
IPADDR=$(ip addr show dev $WIFI_INTERFACE | awk -F '[ /\t]+' '$2=="inet"{print $3; exit}')
IPROUTE=$(echo $IPADDR | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
if [[ "$IPROUTE" != *"$NEW_SUBNET"* ]]; then
echo "$IPROUTE.0/24"
ip route del ${IPROUTE}.0/24
echo "$IPADDR/24"
ip addr del ${IPADDR}/24 dev ${WIFI_INTERFACE}
fi
ndc interface clearaddrs ${WIFI_INTERFACE}
ndc interface setcfg ${WIFI_INTERFACE} ${NEW_SUBNET}.1 24 up
ndc network route add 99 ${WIFI_INTERFACE} ${NEW_SUBNET}.0/24
# Redirect DHCP requests
#iptables -t nat -I PREROUTING -i ${WIFI_INTERFACE} -s 0.0.0.0 -d 255.255.255.255 -p udp -m udp --dport 67 -j DNAT --to-destination 255.255.255.255:1067
# Redirect DNS requests
#iptables -t nat -I PREROUTING -i ${WIFI_INTERFACE} -s ${NEW_SUBNET}.0/24 -d ${NEW_SUBNET}.1 -p udp -m udp --dport 53 -j DNAT --to-destination ${NEW_SUBNET}.1:1053
#kill -9 $(cat /sdcard/dnsmasq.pid)
#dnsmasq --no-resolv --no-poll --dhcp-ignore-names --dhcp-authoritative --dhcp-range=${NEW_SUBNET}.2,${NEW_SUBNET}.99,1h --server=9.9.9.9 --server=1.1.1.1 --dhcp-leasefile=/sdcard/dnsmasq.leases --dhcp-alternate-port=1067,68 --port=1053 --dhcp-option-force=43,ANDROID_METERED --pid-file=/sdcard/dnsmasq.pid --listen-mark 0xf0063 --user dns_tether
exit 0
else
echo "$WIFI_INTERFACE is DOWN!!!"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment