Last active
January 6, 2019 21:08
-
-
Save MartinHarding/f86d44067adff7352f3d8091cf1b666c to your computer and use it in GitHub Desktop.
Configure SSH and WiFi on a newly flashed Rasbpian SD card
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 | |
# Quickly enable SSH and configure WiFi settings on a newly flashed Rasbpian SD card. | |
# Note: this script is meant to be run from the machine used to flash the SD card, not from the Raspberry Pi! | |
# Run script without downloading (Windows users will need either git-bash or cygwin): | |
# bash <(curl -s https://gist.githubusercontent.com/martinharding/f86d44067adff7352f3d8091cf1b666c/raw/rasbpian-configure.sh) | |
# Determine SD card mount point | |
case $OSTYPE in | |
msys | cygwin ) | |
SD_CARD_DEFAULT_MOUNT="D:" | |
SD_CARD_MOUNT_MESSAGE="SD card drive letter (default '$SD_CARD_DEFAULT_MOUNT'): " | |
;; | |
*) | |
SD_CARD_DEFAULT_MOUNT="/Volumes/boot" | |
SD_CARD_MOUNT_MESSAGE="SD card mount path (default '$SD_CARD_DEFAULT_MOUNT'): " | |
;; | |
esac | |
# Ask for SD Card mount point | |
read -p "$SD_CARD_MOUNT_MESSAGE" SD_CARD_MOUNT | |
SD_CARD_MOUNT=${SD_CARD_MOUNT:-$SD_CARD_DEFAULT_MOUNT} | |
# Error checking | |
if [ ! -d $SD_CARD_MOUNT/ ]; then | |
echo "Error: '$SD_CARD_MOUNT' doesn't exist! Is the SD card mounted?" | |
exit 1 | |
fi | |
if [ ! -f $SD_CARD_MOUNT/cmdline.txt ] || [[ -z `cat D:/cmdline.txt | grep raspi` ]]; then | |
echo "Error: '$SD_CARD_MOUNT' doesn't look like a Rasbpian boot partition. Double check the mount path entered." | |
exit 1 | |
fi | |
echo "" | |
# Enable SSH | |
# https://www.raspberrypi.org/documentation/remote-access/ssh/ | |
SSH_ENABLE_PATH=$SD_CARD_MOUNT/ssh | |
if [ -f $SSH_ENABLE_PATH ]; then | |
echo "SSH already enabled." | |
else | |
echo "Enabling SSH" | |
touch $SSH_ENABLE_PATH | |
if [ $? == 0 ]; then | |
echo "Created '$SSH_ENABLE_PATH'" | |
else | |
echo "Could not create '$SSH_ENABLE_PATH'; is the SD card mounted to $SD_CARD_MOUNT?" | |
exit 1 | |
fi | |
fi | |
echo "" | |
# Setup WiFi | |
WPA_SUPPLICANT_PATH=$SD_CARD_MOUNT/wpa_supplicant.conf | |
function setupwifi { | |
echo "Setting up WiFi" | |
read -p "Enter network SSID: " WIFI_SSID | |
read -s -p "Enter network Password: " WIFI_PSK | |
# https://raspberrypi.stackexchange.com/questions/10251/prepare-sd-card-for-wifi-on-headless-pi | |
echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev | |
update_config=1 | |
country=US | |
network={ | |
ssid=\"$WIFI_SSID\" | |
psk=\"$WIFI_PSK\" | |
key_mgmt=WPA-PSK | |
}" > $WPA_SUPPLICANT_PATH | |
if [ $? == 0 ]; then | |
echo -e "\nCreated $WPA_SUPPLICANT_PATH" | |
else | |
echo "Error: Could not create $WPA_SUPPLICANT_PATH; is the SD card mounted to $SD_CARD_MOUNT?" | |
exit 1 | |
fi | |
} | |
if [ -f $WPA_SUPPLICANT_PATH ]; then | |
echo "WiFi already enabled using:" | |
SSID=`cat $WPA_SUPPLICANT_PATH | grep ssid | cut -d= -f2 | tr -d '\n'` | |
echo "Network: ${SSID:1:${#SSID}-2}" | |
PSK=`cat $WPA_SUPPLICANT_PATH | grep psk= | cut -d= -f2 | tr -d '\n'` | |
echo "Password: `printf "%0.s*" $(seq 1 $((${#PSK}-5))); echo ${PSK:1:${#PSK}-2} | tail -c5`" | |
while true; do | |
read -p "Overwrite existing settings (y/n)? " choice | |
case "$choice" in | |
y|Y ) setupwifi; break;; | |
n|N ) echo "Using existing WiFi settings."; break;; | |
* ) echo "Please enter y or n.";; | |
esac | |
done | |
else | |
setupwifi | |
fi | |
echo -e "\nSetup complete. Don't forget to change the SSH password!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment