Skip to content

Instantly share code, notes, and snippets.

@mnl
Last active November 14, 2023 22:07
Show Gist options
  • Save mnl/9cc38d120c5141e264d0a3049d8f4ba2 to your computer and use it in GitHub Desktop.
Save mnl/9cc38d120c5141e264d0a3049d8f4ba2 to your computer and use it in GitHub Desktop.
NetworkManager dispatcher script for wifi toggling
#!/bin/sh
# NetworkManager dispatcher script for /etc/NetworkManager/dispatcher.d/
# Turn off wifi only while ethernet has connection. Adapted from manpage.
# mnl@vandal.nu 2023
ethernet_connected() {
# Check if any ethernet connections are up
nmcli --terse --fields type,state device | while read -r
do
[ "${REPLY}" = ethernet:connected ] && return
done
}
conditionally_toggle_wifi() {
if ethernet_connected
then
nmcli radio wifi off
else
nmcli radio wifi on
fi
}
case ${NM_DISPATCHER_ACTION} in
up|down)
conditionally_toggle_wifi ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment