Skip to content

Instantly share code, notes, and snippets.

@vitouXY
Last active April 8, 2023 03:53
Show Gist options
  • Save vitouXY/fc14c90def6f2cdb731be9a3fa670735 to your computer and use it in GitHub Desktop.
Save vitouXY/fc14c90def6f2cdb731be9a3fa670735 to your computer and use it in GitHub Desktop.
RPi | USB | g_ether
#!/bin/sh
[ $(id -u) = 0 ] || { echo "must be root" ; exit 1; }
set -o errexit
set -o nounset
set -o pipefail
set -o xtrace
[ "$(find /sys/class/net/ -type l -name usb0 2>&-)z" != "z" ] && exit 1
# dwc2 g_ether
micro /etc/modules
# dtoverlay=dwc2
micro /boot/config.txt
# ...rootwait modules-load=dwc2,g_ether
micro /boot/cmdline.txt
cat << EOF | tee /etc/modprobe.d/g_ether.conf > /dev/null
options g_ether use_eem=0
EOF
#micro /etc/modprobe.d/g_ether.conf
/usr/bin/dtoverlay dwc2
/sbin/modprobe dwc2
/sbin/modprobe g_ether
#cat << EOF | tee /etc/dnsmasq_usb0_gether.conf > /dev/null
cat << EOF | tee /etc/dnsmasq.d/usb0_gether.conf > /dev/null
# sudo apt-get install dnsmasq
dhcp-rapid-commit
dhcp-authoritative
no-ping
interface=usb0
except-interface=wlan0
dhcp-range=usb0,10.10.0.2,10.10.0.2,255.255.255.252,1h
domain=gether.usb0
dhcp-option=net:usb0,option:netmask,255.255.255.252
dhcp-option=net:usb0,option:router,10.10.0.1
dhcp-option=usb0,6
#dhcp-option=net:usb0,option:dns-server,8.8.8.8,1.1.1.1
leasefile-ro
addn-hosts=/etc/hosts.usb0
local=/rpi.usb0/
#address=/doubleclick.net/127.0.0.1
EOF
#micro /etc/dnsmasq_usb0_gether.conf
#micro /etc/dnsmasq.d/usb0_gether.conf
cat << EOF | tee /etc/hosts.usb0 > /dev/null
10.10.0.1 rpi.usb0
EOF
cat << EOF | tee /etc/network/interfaces.d/usb0_gether.conf > /dev/null
auto usb0
allow-hotplug usb0
iface usb0 inet static
address 10.10.0.1
netmask 255.255.255.252
EOF
#micro /etc/network/interfaces.d/usb0_gether.conf
# denyinterfaces usb0
micro /etc/dhcpcd.conf
ip link set dev usb0 up
#pgrep dnsmasq || dnsmasq -C /etc/dnsmasq_usb0_gether.conf &
#service dnsmasq enable
service dnsmasq restart
dmesg | grep -iE "g_ether|dwc2|usb0"
exit 0
exit 0
# /29, 255.255.255.248, 0.0.0.7
# /30, 255.255.255.252, 0.0.0.3
## https://www.catalog.update.microsoft.com/Search.aspx?q=USB%20RNDIS%20Gadget
#ip addr add 10.10.0.1/30 broadcast 10.10.0.3 dev usb0
#ip link set dev usb0 up
#ip route add default via 10.10.0.2 dev usb0
cat << EOF | tee /etc/sysctl.d/90-usb0_gether.conf > /dev/null
net.ipv4.ip_forward=1
EOF
cat << EOF | tee /etc/sysctl.d/90-usb0_gether.conf > /dev/null
# sudo apt-get install iptables-persistent
iptables --flush
iptables -t nat --flush
iptables --table nat --append POSTROUTING --out-interface wlan0 -j MASQUERADE
iptables --append FORWARD --in-interface wlan0 --out-interface usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables --append FORWARD --in-interface usb0 --out-interface wlan0 -j ACCEPT
iptables --append FORWARD --in-interface usb0 -j ACCEPT
iptables -t nat -A POSTROUTING -j MASQUERADE
# /etc/iptables/rules.v4
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment