Skip to content

Instantly share code, notes, and snippets.

@lurch
Last active November 1, 2022 16:59
Show Gist options
  • Save lurch/ad939bbce48064cffdb215268eac9f62 to your computer and use it in GitHub Desktop.
Save lurch/ad939bbce48064cffdb215268eac9f62 to your computer and use it in GitHub Desktop.
Script to automatically provide internet access to a PiZero connected to a Linux host over a USB-network (only tested on Ubuntu 14.04)
#!/bin/bash
# Automatically setup routing and DNS for a PiZero connected over a USB-network
# NOTE: Before running this script for the first time, you need to run the
# following two commands on your Linux PC
# sudo sysctl -w net.ipv4.ip_forward=1
# sudo iptables -t nat -A POSTROUTING -s 169.254.0.0/16 -o eth0 -j MASQUERADE
# (replace eth0 in the second command with your internet-facing network device,
# e.g. wlan0 on a laptop)
# The Avahi-discovered hostname
ZERO_HOSTNAME=raspberrypi.local
# The SSH user
ZERO_USERNAME=pi
# The USB network device on the Zero-side (will always be usb0)
ZERO_DEV=usb0
# The USB network device on the PC-side (will probably be usb0)
PC_ZERO_DEV=usb0
# The internet-connected network device on the PC (will probably be eth0 or wlan0)
PC_INET_DEV=$(route | grep ^default | awk '{print $8}')
echo "PC_INET_DEV is $PC_INET_DEV"
ifconfig $PC_ZERO_DEV > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "PC_ZERO_DEV ($PC_ZERO_DEV) doesn't exist"
exit 1
fi
ifconfig $PC_INET_DEV > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "PC_INET_DEV ($PC_INET_DEV) doesn't exist"
exit 1
fi
DNS_SERVER=$(nmcli -t -f IP4 device list iface $PC_INET_DEV | grep DNS | head -1 | cut -d: -f2)
echo "DNS_SERVER is $DNS_SERVER"
# The IP address assigned to the PC-side of the USB network device
PC_ZERO_DEV_IP=$(ifconfig $PC_ZERO_DEV | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}')
echo "PC_ZERO_DEV_IP is $PC_ZERO_DEV_IP"
if [[ "$PC_ZERO_DEV_IP" != 169.254.* ]]; then
echo "PC_ZERO_DEV_IP isn't in the link-local range"
exit 1
fi
# The IP address assigned to Zero-side of the USB network device
ZERO_IP=$(ping -c1 $ZERO_HOSTNAME | grep $ZERO_HOSTNAME | head -1 | cut -d'(' -f2 | cut -d')' -f1)
echo "ZERO_IP is $ZERO_IP"
if [[ "$ZERO_IP" != 169.254.* ]]; then
echo "ZERO_IP isn't in the link-local range"
exit 1
fi
# Setup default route and DNS server on Zero
ssh $ZERO_USERNAME@$ZERO_HOSTNAME "sudo route add default gw $PC_ZERO_DEV_IP $ZERO_DEV; echo \"nameserver $DNS_SERVER\" | sudo resolvconf -a $ZERO_DEV"
@pierrextardif
Copy link

Thks!

Worked for me (ubuntu 16.04) after sharing the wifi connection AND pizero connection, then adding the new route with the right getaway ( one given by the ifconfig of my linux machine).

Picture should be self explonatory, but here is the lines :
ifconfig
enp0s20f0u1 Link encap:Ethernet HWaddr 26:7f:62:5c:48:86 inet addr:10.42.0.1 Bcast:10.42.0.255 Mask:255.255.255.0 inet6 addr: fe80::4c5a:4be4:4b3e:7a1c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:260 errors:0 dropped:0 overruns:0 frame:0 TX packets:525 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:30593 (30.5 KB) TX bytes:70462 (70.4 KB)
ssh pi@raspberrypi.local pi@raspberrypi.local's password:

The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Feb 9 23:51:56 2017 from 10.42.0.1
pi@raspberrypi:~ $sudo route del -net default gw 10.42.0.1 metric 202 netmask 255.255.255.0 dev usb0 pi@raspberrypi:~ $route -n pi@raspberrypi:~ $ sudo systemctl daemon-reload pi@raspberrypi:~ $ sudo service networking restart pi@raspberrypi:~ $ ping 8.8.8.8

self-explainatorypizerousbinternetshare2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment