I wrote a bash script for the Raspberry Pi 3 (Raspbian) which has the main task of setting up a LAN to WLAN router. In addition it makes some things nicer for the intended users who have Windows background only. It will be used on a Pi 3 with built-in WLAN and the official 7" display only.
Preconditions to run the script are (typically done on Windows, so my script can't help here):
- download latest Raspbian
- flash to SD card
- copy 3 files to the /boot partition: the script, a file
asplash
andsplash.png
- insert in Pi and run Raspbian
- configure an Internet connection
- run the script (
/boot/script.sh
)
The script shall
- update the sources (
apt-get update
) - upgrade the installation (
apt-get upgrade
) - install required packages to make all of the following work
- set keyboard to German
- set timezone to Berlin
- forward IPv4 from LAN (eth0) to WLAN (wlan0)
- configure LAN (eth0) to 192.168.63.1 so it can act as a gateway on the 192.168.63.0/24 network
- replace the Raspberry boot output by a nice logo so that the user knows it'll be the right thing on startup
- set the Desktop background by the same nice logo
The order of those things is not really important, but it seems that the Ethernet configuration should only be done when no Internet access is needed any more, since the script might disable Internet access if the original connection was made over LAN.
Yeah, that's it. My tests seem ok, but since this is my first larger bash script, I'd like to get some feedback on how to improve things in the future.
#!/bin/sh
echo "Setting up this device as a LAN to WLAN gateway..."
[ -f /boot/splash.png ] && echo "Splash screen found" || { echo "Please copy splash.png"; exit 1; }
[ -f /boot/asplash ] && echo "Splash screen script found" || { echo "Please copy a file called asplash"; exit 1; }
ping 8.8.8.8 -c 1 -q -w 1 > /dev/null && echo "Internet connection detected" || { echo "Make sure you have a valid Internet connection." ; exit 1; }
echo ""
# Localization --------------------------------------------------------
# Configure timezone to MESZ
# Adjust keyboard to typical German layout
echo "Setting timzone to Europe/Berlin..."
sudo timedatectl set-timezone Europe/Berlin
echo "Setting keyboard to German..."
sudo echo "XKBMODEL=\"pc105\"" | sudo tee /etc/default/keyboard > /dev/null
sudo echo "XKBLAYOUT=\"de\"" | sudo tee --append /etc/default/keyboard > /dev/null
sudo echo "XKBVARIANT=\"deadacute\"" | sudo tee --append /etc/default/keyboard > /dev/null
sudo echo "XKBOPTIONS=\"lv3:ralt_switch,terminate:ctrl_alt_bksp\"" | sudo tee --append /etc/default/keyboard > /dev/null
sudo echo "BACKSPACE=\"guess\"" | sudo tee --append /etc/default/keyboard > /dev/null
# Update to latest versions ------------------------------------------
echo "Updating packages..."
sudo apt-get update
sudo apt-get --yes upgrade
# Install necessary and useful packages -------------------------------
# DNS utilities for Internet diagnosis
sudo apt-get install dnsutils
# Frame buffer viewer For startup splashscreen
sudo apt-get install fbi
# Set up IP Forwarding ------------------------------------------------
# Find net.ipv4.ip_forward and (s)ubstitute it
echo "Setting up IP forwarding..."
sudo sed -i -e '/net.ipv4.ip_forward/ s/.*/net.ipv4.ip_forward=1/' /etc/sysctl.conf
# Set up NAT to route out on WLAN
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
# Make those settings persistent for a restart
mkdir /home/pi/router
sudo iptables-save > /home/pi/router/iptables.tbl
sudo echo "#!/bin/sh" | sudo tee /etc/network/if-pre-up.d/iptables > /dev/null
sudo echo "iptables-restore < /home/pi/router/iptables.tbl" | sudo tee --append /etc/network/if-pre-up.d/iptables > /dev/null
sudo echo "exit 0" | sudo tee --append /etc/network/if-pre-up.d/iptables > /dev/null
sudo chown root:root /etc/network/if-pre-up.d/iptables
sudo chmod +x /etc/network/if-pre-up.d/iptables
sudo chmod 755 /etc/network/if-pre-up.d/iptables
# Set up local network (test net) -------------------------------------
# The LAN interface (eth0) gets a static IP.
# Metric is important, since usually the LAN has lower metric and
# therefore gets a higher precedence. This is exactly what we don't
# want here: traffic must go out on WLAN
echo "Configuring LAN test net to 192.168.63.x ..."
IP=192.168.63.1
sudo echo "interface eth0" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "metric 300" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "static ip_address=$IP/24" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "static routers=$IP" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "static domain_name_servers=$IP" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "interface wlan0" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo echo "metric 200" | sudo tee --append /etc/dhcpcd.conf > /dev/null
sudo systemctl enable dhcpcd
sudo service dhcpcd start
echo "Configuring hostname to 'gateway'..."
sudo echo "gateway" | sudo tee /etc/hostname > /dev/null
# Make booting a bit nicer -------------------------------------------
# Remove the Raspberry logos and the text
# First create a backup copy if it does not exist yet
echo "Removing noise when booting..."
[ -f /boot/cmdline.txt.orig ] || sudo cp /boot/cmdline.txt /boot/cmdline.txt.orig
sudo sed -e '/rootwait/ s/\(.* rootwait\).*/\1 console=tty3 logo.nologo loglevel=3 vt.global_cursor_default=0/' /boot/cmdline.txt.orig | sudo tee /boot/cmdline.txt > /dev/null
# Copy splash screen and script
sudo cp /boot/splash.png /etc/
sudo chmod 644 /etc/splash.png
sudo cp /boot/asplash /etc/init.d/asplash
sudo chmod a+x /etc/init.d/asplash
sudo insserv /etc/init.d/asplash
# Change Desktop background --------------------
echo "Setting Desktop background ..."
sudo cp /boot/splash.png /home/pi/router/
sudo chmod 644 /home/pi/router/splash.png
sudo sed -i -e '/wallpaper=/ s/wallpaper=.*/wallpaper=\/home\/pi\/router\/splash.png/' /home/pi/.config/pcmanfm/LXDE-pi/desktop-items-0.conf