9

How can I disable my Wifi radio when I've connected to a network via Ethernet (wired), but enable the Wifi connection if I don't have a wired connection? Essentially, I want XOR switch for my wired/wireless connection states.

2

1 Answer 1

7

I found a script by Ilija Matoski to accomplish exactly this, which would belong in /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive.sh.

#!/bin/sh

name_tag="wifi-wired-exclusive"
syslog_tag="$name_tag"
skip_filename="/etc/NetworkManager/.$name_tag"

if [ -f "$skip_filename" ]; then
    exit 0
fi

interface="$1"
iface_mode="$2"
iface_type=$(nmcli dev | grep "$interface" | tr -s ' ' | cut -d' ' -f2)
iface_state=$(nmcli dev | grep "$interface" | tr -s ' ' | cut -d' ' -f3)

logger -i -t "$syslog_tag" "Interface: $interface = $iface_state ($iface_type) is $iface_mode"

enable_wifi() {
    logger -i -t "$syslog_tag" "Interface $interface ($iface_type) is down, enabling wifi ..."
    nmcli radio wifi on
}

disable_wifi() {
    logger -i -t "$syslog_tag" "Disabling wifi, ethernet connection detected."
    nmcli radio wifi off
}

if [ "$iface_type" = "ethernet" ] && [ "$iface_mode" = "down" ]; then
    enable_wifi
elif [ "$iface_type" = "ethernet" ] && [ "$iface_mode" = "up"  ] && [ "$iface_state" = "connected" ]; then
    disable_wifi
fi

Additionally, to disable this switching operation, you can create the file /etc/NetworkManager/.wifi-wired-exclusive (e.g. via touch).

2
  • 2
    Since this is a recent answer, is there a systemd solution to the same question?
    – Xofo
    Commented May 10, 2019 at 13:38
  • @Xofo The question specifies NetworkManager, so the systemd solution would be the same. If you mean systemd-networkd, that would be a different question.
    – Headbank
    Commented Aug 3, 2021 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .