Skip to content

Instantly share code, notes, and snippets.

@almir
Last active April 16, 2019 10:40
Show Gist options
  • Save almir/cc32de4fb017575246ff34e4d46c4d1a to your computer and use it in GitHub Desktop.
Save almir/cc32de4fb017575246ff34e4d46c4d1a to your computer and use it in GitHub Desktop.
Disable Wi-Fi when LAN cable is plugged in and enable it when the cable is plugged out
#!/bin/sh
IFACE="${1}"
ACTION="${2}"
ntfy () {
LOGGED_USERS=$(users)
for user in ${LOGGED_USERS}
do
USERID=$(id -u ${user})
sudo -u ${user} \
DISPLAY=:0 \
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${USERID}/bus \
notify-send "${1}" "${2}"
done
}
case ${IFACE} in
eth*|usb*|en* )
case ${ACTION} in
up )
ntfy "Ethernet connected" "Disabling Wi-Fi"
nmcli radio wifi off
;;
down )
ntfy "Ethernet disconnected" "Enabling Wi-Fi"
nmcli radio wifi on
;;
esac
;;
esac
@almir
Copy link
Author

almir commented Apr 15, 2019

This gist is based on https://gist.github.com/cengizIO/c9023b83a8590dd2c723f3181633c99a

The script should be placed under /etc/NetworkManager/dispatcher.d/ with a name like 99-disable-wireless-when-wired.
Make sure that it's owned by root and that it's executable (sudo chown root:root /etc/NetworkManager/dispatcher.d/99-disable-wireless-when-wired && sudo chmod +x /etc/NetworkManager/dispatcher.d/99-disable-wireless-when-wired).
Also, NetworkManager-dispatcher service should be enabled and running for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment