3

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

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).

  • Since this is a recent answer, is there a systemd solution to the same question? – Xofo May 10 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.