I want my TC laptop to automatically connect to wifi on boot, but using only basic tools (no "network manager"). Also, I want a custom icon to show on wbar while there is an internet connection.
Therefore, I wrote these two scripts. I release them into the public domain and post them here in the hope that they are useful to someone. I also welcome criticism since these will be running daily on my machine.
Here is the first script. I named it autowifi and it runs once at boot. The script contains a list of the hotspots I use, in order of preference (most to least preferred). The laptop connects to the first of the listed hotspots that it finds.
#!/bin/sh
# Purpose: Run this at boot as root (e.g., via /opt/bootlocal.sh) for wifi autoconnect.
# Dependency: wpa_supplicant.tcz (which pulls in everything else that's needed).
# user variables:
iface=wlan0
main()
{
# internal variables:
scan_results=/tmp/wifi-scan-results.txt
auth_file=/tmp/wifi.conf
scan
# ssids are searched in order, so first one found wins:
ssid="AndroidAP"; password="somEpaSsword"; search && connect
ssid="home_sweet_home"; password="HoMePasswoRd"; search && connect
ssid="openCafeNetwork"; password=""; search && connect
}
scan()
{
ifconfig $iface up
iwlist scanning > $scan_results
}
search()
{
grep -q "$ssid" $scan_results && return 0 || return 1
}
connect()
{
if [ -z "$password" ]; then # there's no password
iwconfig $iface essid $ssid &
else # there's a password
echo 'ctrl_interface=/var/run/wpa_supplicant' > $auth_file
wpa_passphrase "$ssid" "$password" >> $auth_file
wpa_supplicant -i $iface -c $auth_file &
fi
sleep 5
udhcpc -i $iface
exit
}
main
Here is the second script. I named it network-monitor. It is started at boot and runs continuously in the background, checking for an internet connection every 10 seconds. It shows/hides a custom .png icon in my wbar as appropriate.
#!/bin/sh
# Purpose: Start this at boot as regular user (e.g., via ~/X.d/) for custom icon to show on wbar while there is internet access.
# Dependency: wbar.tcz
# user variables:
icon_dir=/opt/media
icon=online.png
main()
{
# internal variables:
wbar_config_file=$HOME/.wbar
while true; do
if timeout -t 5 wget --spider duckduckgo.com >/dev/null 2>&1; then
show_icon
else
hide_icon
fi
sleep 10
done
}
show_icon()
{
fgrep -q "i: $icon_dir/$icon" $wbar_config_file && return # if icon already present, we have nothing to do
echo "i: $icon_dir/$icon
t: $icon
c: $icon" >> $wbar_config_file
restart_wbar
}
hide_icon()
{
fgrep -q "i: $icon_dir/$icon" $wbar_config_file || return # if icon already absent, we have nothing to do
sed -i "/$icon/ d" $wbar_config_file
restart_wbar
}
restart_wbar()
{
pkill wbar
wbar &
}
main