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,241 +41,315 @@ 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
passphrase=$2 passphrase=$2
psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}') psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
echo $psk echo $psk
} }
function hotspot_off { function hotspot_off {
if [ ! -f /etc/hostapd/hostapd.conf ]; then if [ ! -f /etc/hostapd/hostapd.conf ]; then
return return
fi fi
systemctl stop hostapd systemctl stop hostapd
rm /etc/hostapd/hostapd.conf rm /etc/hostapd/hostapd.conf
if [ -f /etc/network/interfaces_original ]; then if [ -f /etc/network/interfaces_original ]; then
cp /etc/network/interfaces_original /etc/network/interfaces cp /etc/network/interfaces_original /etc/network/interfaces
else else
echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
fi fi
systemctl restart network-manager systemctl restart network-manager
} }
function hotspot_on { function hotspot_on {
if [ ! -f /etc/default/hostapd ]; then if [ ! -f /etc/default/hostapd ]; then
echo $'/etc/default/hostapd was not found' echo $'/etc/default/hostapd was not found'
exit 67241 exit 67241
fi fi
if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
echo $'Wifi hotspot passphrase is too short' echo $'Wifi hotspot passphrase is too short'
exit 25719 exit 25719
fi fi
sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
echo '' >> /etc/hostapd/hostapd.conf echo '' >> /etc/hostapd/hostapd.conf
echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
echo 'bridge=br0' >> /etc/hostapd/hostapd.conf echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
echo '' >> /etc/hostapd/hostapd.conf echo '' >> /etc/hostapd/hostapd.conf
echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
echo "country_code=UK" >> /etc/hostapd/hostapd.conf echo "country_code=UK" >> /etc/hostapd/hostapd.conf
echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
echo 'channel=6' >> /etc/hostapd/hostapd.conf echo 'channel=6' >> /etc/hostapd/hostapd.conf
echo 'wpa=2' >> /etc/hostapd/hostapd.conf echo 'wpa=2' >> /etc/hostapd/hostapd.conf
echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
echo '' >> /etc/hostapd/hostapd.conf echo '' >> /etc/hostapd/hostapd.conf
echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
echo '' >> /etc/hostapd/hostapd.conf echo '' >> /etc/hostapd/hostapd.conf
echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
echo '' >> /etc/hostapd/hostapd.conf echo '' >> /etc/hostapd/hostapd.conf
echo '## Shared Key Authentication ##' echo '## Shared Key Authentication ##'
echo 'auth_algs=1' echo 'auth_algs=1'
echo '' >> /etc/hostapd/hostapd.conf echo '' >> /etc/hostapd/hostapd.conf
echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
if [ ! -f /etc/network/interfaces_original ]; then if [ ! -f /etc/network/interfaces_original ]; then
if ! grep -q "# wifi enabled" /etc/network/interfaces; then if ! grep -q "# wifi enabled" /etc/network/interfaces; then
cp /etc/network/interfaces /etc/network/interfaces_original cp /etc/network/interfaces /etc/network/interfaces_original
fi fi
fi fi
echo '# wifi enabled' > /etc/network/interfaces echo '# wifi enabled' > /etc/network/interfaces
echo 'auto lo br0' >> /etc/network/interfaces echo 'auto lo br0' >> /etc/network/interfaces
echo 'iface lo inet loopback' >> /etc/network/interfaces echo 'iface lo inet loopback' >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces
echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces
echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo '# eth0 connected to the ISP router' >> /etc/network/interfaces echo '# eth0 connected to the ISP router' >> /etc/network/interfaces
echo 'allow-hotplug eth0' >> /etc/network/interfaces echo 'allow-hotplug eth0' >> /etc/network/interfaces
echo 'iface eth0 inet manual' >> /etc/network/interfaces echo 'iface eth0 inet manual' >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo '# Setup bridge' >> /etc/network/interfaces echo '# Setup bridge' >> /etc/network/interfaces
echo 'iface br0 inet static' >> /etc/network/interfaces echo 'iface br0 inet static' >> /etc/network/interfaces
echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces
systemctl restart network-manager systemctl restart network-manager
systemctl restart hostapd systemctl restart hostapd
} }
function wifi_wpa2_psk { function wifi_wpa2_psk {
ssid=$1 ssid=$1
passphrase=$2 passphrase=$2
if [ ! -f /etc/network/interfaces_original ]; then if [ ! -f /etc/network/interfaces_original ]; then
if ! grep -q "# wifi enabled" /etc/network/interfaces; then if ! grep -q "# wifi enabled" /etc/network/interfaces; then
cp /etc/network/interfaces /etc/network/interfaces_original cp /etc/network/interfaces /etc/network/interfaces_original
fi fi
fi fi
echo '# wifi enabled' > /etc/network/interfaces echo '# wifi enabled' > /etc/network/interfaces
echo 'auto lo' >> /etc/network/interfaces echo 'auto lo' >> /etc/network/interfaces
echo 'iface lo inet loopback' >> /etc/network/interfaces echo 'iface lo inet loopback' >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo 'allow-hotplug eth0' >> /etc/network/interfaces echo 'allow-hotplug eth0' >> /etc/network/interfaces
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
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
} }
function wifi_none { function wifi_none {
ssid=$1 ssid=$1
if [ ! -f /etc/network/interfaces_original ]; then if [ ! -f /etc/network/interfaces_original ]; then
if ! grep -q "# wifi enabled" /etc/network/interfaces; then if ! grep -q "# wifi enabled" /etc/network/interfaces; then
cp /etc/network/interfaces /etc/network/interfaces_original cp /etc/network/interfaces /etc/network/interfaces_original
fi fi
fi fi
echo '# wifi enabled' > /etc/network/interfaces echo '# wifi enabled' > /etc/network/interfaces
echo 'auto lo' >> /etc/network/interfaces echo 'auto lo' >> /etc/network/interfaces
echo 'iface lo inet loopback' >> /etc/network/interfaces echo 'iface lo inet loopback' >> /etc/network/interfaces
echo '' >> /etc/network/interfaces echo '' >> /etc/network/interfaces
echo 'allow-hotplug eth0' >> /etc/network/interfaces echo 'allow-hotplug eth0' >> /etc/network/interfaces
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
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 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
} }
function show_help { function show_help {
echo '' echo ''
echo $"${PROJECT_NAME}-wifi -i [interface] -t [type] -s [ssid] -p [passphrase]" echo $"${PROJECT_NAME}-wifi -i [interface] -t [type] -s [ssid] -p [passphrase]"
echo '' echo ''
echo $'Wifi configuration tool' echo $'Wifi configuration tool'
echo '' echo ''
echo $' --help Show help' echo $' --help Show help'
echo $' -i --interface [wlan0|wlan1...] Device name' echo $' -i --interface [wlan0|wlan1...] Device name'
echo $' -t --type [wpa2-psk|none] Security type' echo $' -t --type [wpa2-psk|none] Security type'
echo $' -s --ssid [id] Set SSID' echo $' -s --ssid [id] Set SSID'
echo $' -p --passphrase [text] Set passphrase' echo $' -p --passphrase [text] Set passphrase'
echo $' --hotspot [yes|no] Create a hotspot' echo $' --hotspot [yes|no] Create a hotspot'
echo '' echo ''
exit 0 exit 0
} }
while [[ $# > 1 ]] while [[ $# > 1 ]]
do do
key="$1" key="$1"
case $key in case $key in
--help) --help)
show_help show_help
;; ;;
-i|--if|--interface) -i|--if|--interface)
shift shift
WIFI_INTERFACE=${1} WIFI_INTERFACE=${1}
;; ;;
-t|--type) -t|--type)
shift shift
WIFI_TYPE=${1} WIFI_TYPE=${1}
;; ;;
-s|--ssid) -s|--ssid)
shift shift
WIFI_SSID=${1} WIFI_SSID=${1}
;; ;;
-p|--pass|--passphrase) -p|--pass|--passphrase)
shift shift
WIFI_PASSPHRASE=${1} WIFI_PASSPHRASE=${1}
;; ;;
--hostpot) --hostpot)
shift shift
WIFI_HOTSPOT=${1} WIFI_HOTSPOT=${1}
;; ;;
*) --config)
# unknown option shift
;; WIFI_FILE=${1}
esac ;;
shift *)
# unknown option
;;
esac
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
fi fi
if [[ $WIFI_HOTSPOT != 'no' ]]; then if [[ $WIFI_HOTSPOT != 'no' ]]; then
hotspot_on hotspot_on
exit 0 exit 0
else else
hotspot_off hotspot_off
fi fi
if [[ $WIFI_TYPE != 'none' ]]; then if [[ $WIFI_TYPE != 'none' ]]; then
if [ ! $WIFI_PASSPHRASE ]; then if [ ! $WIFI_PASSPHRASE ]; then
echo $'No wifi passphrase was given' echo $'No wifi passphrase was given'
exit 2 exit 2
fi fi
fi fi
if [[ $WIFI_TYPE == 'wpa2-psk' ]]; then if [[ $WIFI_TYPE == 'wpa2-psk' ]]; then
if [ ! -d /etc/wpa_supplicant ]; then if [ ! -d /etc/wpa_supplicant ]; then
echo $'wpasupplicant package is not installed' echo $'wpasupplicant package is not installed'
exit 3 exit 3
fi fi
wifi_wpa2_psk "$WIFI_SSID" "$WIFI_PASSPHRASE" wifi_wpa2_psk "$WIFI_SSID" "$WIFI_PASSPHRASE"
exit 0 exit 0
fi fi
if [[ $WIFI_TYPE == 'none' ]]; then if [[ $WIFI_TYPE == 'none' ]]; then
wifi_none "$WIFI_SSID" wifi_none "$WIFI_SSID"
exit 0 exit 0
fi fi
exit 0 exit 0