Last active
February 16, 2017 23:39
Star
You must be signed in to star a gist
Connect raspberry pizero to internet. Based on Andrew Scheller script -> https://gist.github.com/lurch/ad939bbce48064cffdb215268eac9f62
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
PC_ZERO_DEV=enp0s20f0u1 | |
# 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 show | 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 | |
sudo iptables -A FORWARD -o eth0 -i enp0s20f0u1 -s 169.254.190.0/16 -m conntrack --ctstate NEW -j ACCEPT | |
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT | |
sudo iptables -t nat -F POSTROUTING | |
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | |
# 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment