Read wifi networks from file

This commit is contained in:
Bob Mottram 2016-04-26 19:50:01 +01:00
parent 2e2c607112
commit 494af60013
1 changed files with 260 additions and 186 deletions

View File

@ -41,6 +41,8 @@ WIFI_TYPE='wpa2-psk'
WIFI_SSID= WIFI_SSID=
WIFI_PASSPHRASE= WIFI_PASSPHRASE=
WIFI_HOTSPOT='no' WIFI_HOTSPOT='no'
WIFI_CONFIG=/etc/wpa_supplicant/wpa_supplicant.conf
WIFI_FILE=/root/${PROJECT_NAME}-wifi.cfg
function wifi_get_psk { function wifi_get_psk {
ssid=$1 ssid=$1
@ -155,11 +157,11 @@ function wifi_wpa2_psk {
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo 'allow-hotplug wlan0' >> /etc/network/interfaces echo 'allow-hotplug wlan0' >> /etc/network/interfaces
echo 'iface wlan0 inet manual' >> /etc/network/interfaces echo 'iface wlan0 inet manual' >> /etc/network/interfaces
echo 'wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf' >> /etc/network/interfaces echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo 'iface default inet dhcp' >> /etc/network/interfaces echo 'iface default inet dhcp' >> /etc/network/interfaces
wpa_passphrase "$ssid" "$passphrase" > /etc/wpa_supplicant/wpa_supplicant.conf wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
systemctl restart network-manager systemctl restart network-manager
} }
@ -182,14 +184,77 @@ function wifi_none {
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo 'allow-hotplug wlan0' >> /etc/network/interfaces echo 'allow-hotplug wlan0' >> /etc/network/interfaces
echo 'iface wlan0 inet manual' >> /etc/network/interfaces echo 'iface wlan0 inet manual' >> /etc/network/interfaces
echo 'wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf' >> /etc/network/interfaces echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo 'iface default inet dhcp' >> /etc/network/interfaces echo 'iface default inet dhcp' >> /etc/network/interfaces
echo 'network={' > /etc/wpa_supplicant/wpa_supplicant.conf echo 'network={' > $WIFI_CONFIG
echo " ssid=\"${ssid}\"" >> /etc/wpa_supplicant/wpa_supplicant.conf echo " ssid=\"${ssid}\"" >> $WIFI_CONFIG
echo ' key_mgmt=NONE' >> /etc/wpa_supplicant/wpa_supplicant.conf echo ' key_mgmt=NONE' >> $WIFI_CONFIG
echo '}' >> /etc/wpa_supplicant/wpa_supplicant.conf echo '}' >> $WIFI_CONFIG
systemctl restart network-manager
}
function create_networks_from_file {
if [ ! -f $WIFI_FILE ]; then
return
fi
if [ ! -f /etc/network/interfaces_original ]; then
if ! grep -q "# wifi enabled" /etc/network/interfaces; then
cp /etc/network/interfaces /etc/network/interfaces_original
fi
fi
echo '# wifi enabled' > /etc/network/interfaces
echo 'auto lo' >> /etc/network/interfaces
echo 'iface lo inet loopback' >> /etc/network/interfaces
echo '' >> /etc/network/interfaces
echo 'allow-hotplug eth0' >> /etc/network/interfaces
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
echo '' >> /etc/network/interfaces
echo 'allow-hotplug wlan0' >> /etc/network/interfaces
echo 'iface wlan0 inet manual' >> /etc/network/interfaces
echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
echo '' >> /etc/network/interfaces
echo 'iface default inet dhcp' >> /etc/network/interfaces
# remove wpa_supplicant.conf if it exists
if [ -f $WIFI_CONFIG ]; then
rm -f $WIFI_CONFIG
fi
ctr=0
while read -r line
do
if [ ${#line} -gt 1 ]; then
if [[ "$line" != '#'* ]]; then
if [ $ctr -eq 0 ]; then
WIFI_SSID="$line"
fi
if [ $ctr -eq 1 ]; then
WIFI_TYPE="$line"
if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'None' ]]; then
echo 'network={' >> $WIFI_CONFIG
echo " ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
echo ' key_mgmt=NONE' >> $WIFI_CONFIG
echo '}' >> $WIFI_CONFIG
ctr=0
continue
fi
fi
if [ $ctr -eq 2 ]; then
WIFI_PASSPHRASE="$line"
wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
ctr=0
continue
fi
ctr=$((ctr + 1))
fi
fi
done < $WIFI_FILE
systemctl restart network-manager systemctl restart network-manager
} }
@ -238,6 +303,10 @@ do
shift shift
WIFI_HOTSPOT=${1} WIFI_HOTSPOT=${1}
;; ;;
--config)
shift
WIFI_FILE=${1}
;;
*) *)
# unknown option # unknown option
;; ;;
@ -245,6 +314,11 @@ do
shift shift
done done
if [ -f $WIFI_FILE ]; then
create_networks_from_file
exit 0
fi
if [ ! $WIFI_SSID ]; then if [ ! $WIFI_SSID ]; then
echo $'No SSID given' echo $'No SSID given'
exit 1 exit 1