Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Netmounts trayicon

In another cute and dangerous way to loop status checks, here is my netmounts-trayicon script. My goal with this tool is to provide a system tray icon that lets me easily umount and mount my nfs exports. I probably should learn to use autofs, but that's a pain, and I'm convinced there's something wrong with the Debian-based autofs daemon.

I wrote this because when I switch from wired to wireless networking to take my laptop away from my desk, the nfs mounts don't survive changing network cards. And nfs timeouts are notoriously long (5 or 10 minutes or something), so I would rather just choose to unmount the shares, and then re-mount them.

In the script, I loop through grepping the output of the mount command and update my capital-M/lowercase-M tray icon. I also set it to have the tooltip contents of the list of current nfs mounts. Again, I used mktrayicon, with very simple icons, stylized lower- and upper-case letter "M." I got the icons under a linkware license from visualpharm.com.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh
# Startdate: 2021-12-26 21:10
# Reference: keyboard-leds-trayicons
# Documentation:
#    for some stupid reason sudo /usr/local/bin/vpn-on doesn't work, so I just use the real commands here.

clean_vpn_trayicon() {
   { test -e "${vpn_trayicon}" && echo "q" > "${vpn_trayicon}" ; } 1>/dev/null 2>&1 &
   sleep 1 && rm -f "${vpn_trayicon}" "${vpn_KILLFILE}"
}

export vpn_trayicon="/var/run/user/$( id -u )/${$}.vpn.icon"
export vpn_KILLFILE=/tmp/kill-all-vpn-trayicons

test "ON" = "ON" && {
   mkfifo "${vpn_trayicon}"
   mktrayicon "${vpn_trayicon}" &
   echo "m Turn vpn on,sudo wg-quick up wg0|Turn vpn off,sudo wg-quick down wg0|quit,echo 'q' > ${vpn_trayicon} ; touch \"${vpn_KILLFILE}\"" > "${vpn_trayicon}"
   echo "i networkmanager" > "${vpn_trayicon}"
}

rm -f "${vpn_KILLFILE}"

trap 'trap "" 2 ; touch "${vpn_KILLFILE}" '  2 # CTRL-C

while ! test -e "${vpn_KILLFILE}" 2>/dev/null ;
do
   ip -o a s wg0 1>/dev/null 2>&1 ; status_now=$? ;
   if test "${status_now}" != "${status_old}" ;
   then
      test -p "${vpn_trayicon}" && case "${status_now}" in
         0) # vpn is on now
            test -n "${VPN_DEBUG}" && echo "vpn is on (icon file ${vpn_trayicon})" 1>&2
            echo "i /usr/local/share/vpn-on.svg" > "${vpn_trayicon}"
            echo "t vpn is on" > "${vpn_trayicon}"
            ;;
         1) # vpn is off now
            test -n "${VPN_DEBUG}" && echo "vpn is off (icon file ${vpn_trayicon})" 1>&2
            echo "i /usr/local/share/vpn-off.svg" > "${vpn_trayicon}"
            echo "t vpn is off" > "${vpn_trayicon}"
            ;;
      esac
   fi
   status_old="${status_now}"
   sleep 1
done

# safety shutoff
clean_vpn_trayicon

And my /usr/local/bin/netmounts-on file:

1
2
#!/bin/sh
sudo mount -av -t nfs

And netmounts-off script.

1
2
3
#!/bin/sh
mounts="$( mount | awk '$5~/nfs/{print $3}' )"
for word in ${mounts} ; do sudo umount -lv "${word}" ; done

Comments