Report comment

Hi Graeme, here is another update. I have some hidden networks in my configuration, so I also wanted to scan for some mac addresses. In addition, I want to have an e-mail with the current IP address if connection was established to a wifi network or personal hotspot of my phone or tablet.

Please see the modified script below, Cheers.

#!/bin/bash
##
# Wifi config - if no prefered Wifi generate a hotspot
##

## enter required ssids: ssids=('ssid1' 'ssid2') ...

#ssids=('mySSID1' 'mySSID2')

## ... or let the script parse your wpa_supplicant.conf

ssids=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=' ' | sed 's/\"/''/g' | sed 's/ $//')

## for hidden wifi networks, add mac adresses here:

macAdresses=('11:22:33:44:55:66' '66:55:44:33:22:11')

## build network string
networks=($ssids ${macAdresses[@]})

createAdHocNetwork()
{
ip link set dev wlan0 down
ip a add 10.0.0.5/24 dev wlan0
ip link set dev wlan0 up
systemctl start dnsmasq
systemctl start hostapd
}
KillHotspot()
{
echo "Shutting Down Hotspot"
ip link set dev wlan0 down
systemctl stop hostapd
systemctl stop dnsmasq
ip addr flush dev wlan0
ip link set dev wlan0 up
}
#check for entry in range
ssidChk=('NoSSid')
for entry in "${networks[@]}"
do
if iw dev wlan0 scan ap-force | grep "$entry" > /dev/null ;then
ssidChk=$entry
break
else
ssidChk='NoSSid'
fi
done

if [ $ssidChk != "NoSSid" ]
then
if systemctl status hostapd | grep "(running)"
then #hotspot running and network entry in range
KillHotspot
echo "Hotspot Deactivated, Bringing Wifi Up"
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
# wait and check if wifi connection was established
sleep 10
if wpa_cli status | grep 'wlan0' > /dev/null
then
echo "Wifi connection ok"
# send e-mail with ip address
echo "Wifi connection established..." |mail -s "[WifiConnect] $(hostname) - IP: $(hostname -I|cut -d" " -f1) / `date +\%Y.\%m.\%d` - `date +\%H:\%M` "
else
echo "Wifi failed to connect, Creating Hotspot again"
wpa_cli terminate
createAdHocNetwork
fi
elif wpa_cli status | grep 'wlan0'
then #If already connected stop script
echo "Wifi already connected to network"
else #network entry exists and no hotspot running connect to wifi network
echo "Connecting to WiFi Network"
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
if wpa_cli status | grep 'wlan0' > /dev/null
then
echo "Wifi connection ok"
# send e-mail with ip address
echo "Wifi connection established..." |mail -s "[WifiConnect] $(hostname) - IP: $(hostname -I|cut -d" " -f1) / `date +\%Y.\%m.\%d` - `date +\%H:\%M` "
else
echo "Wifi failed to connect, Creating Hotspot instead"
wpa_cli terminate
createAdHocNetwork
fi
fi
else #no network entry in range
if systemctl status hostapd | grep "(running)" > /dev/null
then
echo "Hostspot already active"
elif wpa_cli status | grep 'wlan0' > /dev/null
then
echo "Cleaning wifi files and Activating Hotspot"
wpa_cli terminate
ip addr flush wlan0
ip link set dev wlan0 down
rm -r /var/run/wpa_supplicant > /dev/null
createAdHocNetwork
else #"No network entry in range, activating Hotspot"
createAdHocNetwork
fi
fi