WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: autowifi and wifi-monitor scripts  (Read 7160 times)

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1509
autowifi and wifi-monitor scripts
« on: July 27, 2019, 09:41:43 AM »
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.

Code: [Select]
#!/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.

Code: [Select]
#!/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

« Last Edit: October 07, 2019, 02:13:02 PM by Rich »

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1509
autowifi and wifi-monitor scripts
« Reply #1 on: July 29, 2019, 08:58:20 AM »
I simplified the logic of network-monitor a bit, and found a more responsive webpage to use as a check.

Code: [Select]
#!/bin/sh

# Purpose: Run 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/scripts/media
icon=online.png
wbar_config_file=$HOME/.wbar

icon_add()
{
echo "i: $icon_dir/$icon
t: $icon
c: $icon" >> $wbar_config_file
pkill wbar && wbar &
}

icon_remove()
{
sed -i "/$icon/ d" $wbar_config_file
pkill wbar && wbar &
}

while true; do
if timeout -t 10 wget -q --spider http://ipinfo.io 2>/dev/null; then
grep -q "$icon" "$wbar_config_file" || icon_add
else
grep -q "$icon" "$wbar_config_file" && icon_remove
fi
sleep 10
done

P.S. I'm using wget instead of ping for two reasons:
1. My workplace blocks ICMP traffic for some strange reason
2. wgetting a webpage is a more robust check for internet access, as it also tells me that DNS is working

If anyone knows of an even smaller and faster webpage to use as a check--preferably one that has been around for years and is more or less guaranteed to always be available--please let me know (note: I'm intentionally avoiding google. We are a google/apple/microsoft/facebook-free household).
« Last Edit: October 07, 2019, 02:13:16 PM by Rich »

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1509
autowifi and wifi-monitor scripts
« Reply #2 on: October 06, 2019, 11:38:42 PM »
I gave these shell scripts (now called autowifi and wifi-monitor) a lot of polish and have made them minimalistic and OS-agnostic:

https://github.com/bdantas/autowifi
https://github.com/bdantas/wifi-monitor

To use them on TC:
1. $ tce-load -wi curl yad # wpa_supplicant, which you already have if you use wifi, pulls in everything else as dependencies
2. Download the scripts, put them somewhere in your PATH, make them executable, verify the user variables at the top
3. Create ~/.X.d/wifi.sh (or similar), make it executable, put this in it:
Code: [Select]
sudo autowifi &
wifi-monitor &
4. Reboot.

These tiny shell scripts have made bloated "network managers" a thing of the past for me. I hope someone finds them useful.
« Last Edit: October 07, 2019, 02:13:40 PM by Rich »

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1509
autowifi and wifi-monitor scripts
« Reply #3 on: October 07, 2019, 01:22:24 PM »
@Rich - Would it be possible to change the title of the thread (to "autowifi and wifi-monitor scripts") and to delete the first two posts? Feel free to also delete this post.
« Last Edit: October 07, 2019, 02:13:50 PM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11618
Re: autowifi and wifi-monitor scripts
« Reply #4 on: October 07, 2019, 02:20:58 PM »
@Rich - Would it be possible to change the title of the thread (to "autowifi and wifi-monitor scripts" ... )
Done.

Quote
... and to delete the first two posts? Feel free to also delete this post.
I prefer not to delete the documentation of how we got to where we are.

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1509
Re: autowifi and wifi-monitor scripts
« Reply #5 on: October 07, 2019, 02:22:59 PM »
Thank you, Rich. That makes sense.