Last active
April 25, 2019 13:34
-
-
Save waxb/363920c1ce95b0340c34ad6a3880e563 to your computer and use it in GitHub Desktop.
Raspberry Pi Wi-Fi join script
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 | |
# Raspbian Wi-Fi connect script | |
# You can scan [1], connect directly [2], rejoin [3] or view the configuration file [4] with this little script I wrote just for fun and I thought I'd share it :-) | |
# Tested on Raspberry Pi Zero w | |
if [ "$(id -u)" != "0" ] ; then | |
echo "Please run this script as superuser" | |
exit 1 | |
fi | |
if [ -z "$(which wpa_cli)" ] ; then | |
echo "wpa_cli: not found. Exiting." | |
exit 1 | |
fi | |
if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then | |
echo -e "Raspbian Wi-Fi connect script: no options yet" | |
fi | |
ConnectToSSID() { | |
SSID=${1} | |
PASSWORD=${2:-'key_mgmt=NONE'} | |
CONFIG="/etc/wpa_supplicant/wpa_supplicant.conf" | |
if [ ! -z "$isEncrypted" ] ; then | |
NETWORK="$(wpa_passphrase "${SSID}" "${PASSWORD}" | grep -v "#psk")" | |
else | |
NETWORK="network={\n ssid="\""${SSID}"\""\n "psk=\"${PASSWORD}\""\n}" | |
fi | |
if [ ! -z "$(cat /etc/wpa_supplicant/wpa_supplicant.conf | grep "$SSID")" ] ; then | |
while : ; do | |
echo -ne "This network is already added to the configuration\nAdd it again? [y\N]: " #TODO: add option to replace old SSID lines | |
read redo | |
case "$redo" in | |
y|Y) | |
break | |
;; | |
n|N|"") | |
exit | |
;; | |
*) | |
continue | |
;; | |
esac | |
done | |
fi | |
echo -e "$NETWORK" >> $CONFIG | |
} | |
CheckWPA() { | |
if [ ! -z "$(iwlist wlan0 scan | sed -nE "/$1/,/SSID/p" | grep WPA)" ] ; then | |
echo -ne "It seems like this network is password protected (NOTE: this script doesn't checks if the password is correct). Please type the password: " | |
while : ; do | |
read -s wifipassword | |
if [ ${#wifipassword} -lt 8 ] ; then | |
echo "\nThe password should be 8 or more characters" | |
continue | |
fi | |
echo -ne "\nDo you want to store the password encrypted only?: [Y/n]: " | |
read enc | |
case "$enc" in | |
y|Y|"") | |
isEncrypted=true | |
;& | |
n|N) | |
return | |
;; | |
*) | |
continue | |
;; | |
esac | |
done | |
fi | |
} | |
ScanWifi() { #TODO: refine WPA detection | |
while : ; do | |
echo "These are the available SSIDs" | |
iwlist wlan0 scan | grep SSID | |
echo -ne "Type the SSID to connect or press RETURN for rescan: " | |
read option | |
if [ ! -z "$option" ] ; then | |
return | |
fi | |
done | |
} | |
while : ; do | |
echo -ne "Please select:\n Scan for active Wi-Fi routers [1]\n Type SSID to connect [2]\n Rejoin [3]\n View config file [4]\n Exit [0]\n Select: " | |
read answer | |
case "$answer" in | |
1) | |
ScanWifi | |
;& | |
2) | |
if [ -z "$option" ] ; then | |
echo -n "Type the SSID: " | |
read option | |
fi | |
CheckWPA "$option" | |
ConnectToSSID "$option" "$wifipassword" | |
;& | |
3) | |
wpa_cli -i wlan0 reconfigure | |
;& | |
0) | |
exit | |
;; | |
4) | |
cat /etc/wpa_supplicant/wpa_supplicant.conf | |
continue | |
;; | |
*) | |
echo "Please select: " | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment