Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmi1974/d39d2550d3fe8e685a3c46bc8489ca04 to your computer and use it in GitHub Desktop.
Save rmi1974/d39d2550d3fe8e685a3c46bc8489ca04 to your computer and use it in GitHub Desktop.
How to automatically enable and disable WiFi based on Ethernet connection with NetworkManager #wifi #networkmanager #commandlinefu

How to automatically enable and disable WiFi based on Ethernet connection with NetworkManager

Courtesy of distroguy blog post

cat << \EOF |sudo tee /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive.sh
#!/bin/bash
export LC_ALL=C

enable_disable_wifi ()
{
    result=$(nmcli dev | grep "ethernet" | grep -w "connected")
    if [ -n "$result" ]; then
        nmcli radio wifi off
    else
        nmcli radio wifi on
    fi
}

if [ "$2" = "up" ]; then
    enable_disable_wifi
fi

if [ "$2" = "down" ]; then
    enable_disable_wifi
fi
EOF
sudo chown root:root /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive.sh
sudo chmod 744 /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive.sh
sudo systemctl restart NetworkManager

Links

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