Skip to content

Instantly share code, notes, and snippets.

@luisalima
Created February 21, 2019 10:44
Show Gist options
  • Save luisalima/c6fe24c6e5878d3a6c027fcd53591b13 to your computer and use it in GitHub Desktop.
Save luisalima/c6fe24c6e5878d3a6c027fcd53591b13 to your computer and use it in GitHub Desktop.
A work in progress script to provision a raspberry pi with raspbian
#!/bin/bash
export HOSTNAME='***'
TOOLS="tmux"
COUNTRY="**"
SSID="***"
function change_hostname {
sudo sh -c "echo $HOSTNAME > /etc/hostname"
sudo sed -i "s/raspberrypi/$HOSTNAME/g" /etc/hosts
sudo sh -c "echo 127.0.0.1 $HOSTNAME >> /etc/hosts"
}
function enable_sshd {
sudo systemctl enable ssh.service
sudo systemctl start ssh.service
}
function update_packages {
sudo apt-get update
}
function install_tools {
sudo apt-get install -y $TOOLS
}
function update_locales {
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8
}
function configure_wlan0 {
FILE="/etc/wpa_supplicant/wpa_supplicant.conf"
sudo grep -q "country=$COUNTRY" "$FILE" || echo "country=$COUNTRY" | sudo tee -a "$FILE"
if ! sudo grep -q "$SSID" "$FILE"; then
echo "Enter password for $SSID:"
NETINFO=$(wpa_passphrase "$SSID")
PARSEDNETINFO=$(echo "$NETINFO" | sed '/#/ d' )
sudo grep -q "$PARSEDNETINFO" "$FILE" || echo "$PARSEDNETINFO" | sudo tee -a "$FILE"
else
echo "$SSID network configuration detected in $FILE, skipping..."
fi
sudo wpa_cli -i wlan0 reconfigure
sudo dhcpcd wlan0
}
# not necessary in most recent raspbian versions
# see https://raspberrypi.stackexchange.com/a/37921
function configure_dhcp {
DHCP_CONFIG=$(cat <<_EOF_
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
_EOF_)
FILE="/etc/network/interfaces"
sudo grep -q "$DHCP_CONFIG" "$FILE" || echo "$DHCP_CONFIG" | sudo tee -a "$FILE"
sudo dhcpcd -k wlan0
sudo dhcpcd wlan0
}
function test_wlan0 {
ifconfig wlan0
route -n
ip rule show
ip route show table local
}
function change_password {
passwd pi
}
###
change_hostname
enable_sshd
update_locales
update_packages
install_tools
configure_wlan0
test_wlan0
change_password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment