如何在通过以太网(有线)连接到网络时禁用 Wifi 无线电,但在没有有线连接时启用 Wifi 连接?本质上,我想要 XOR 开关来表示我的有线/无线连接状态。
答案1
我发现伊利亚·马托斯基 剧本正是为了实现这一点,这将属于/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
此外,要禁用此切换操作,您可以创建该文件/etc/NetworkManager/.wifi-wired-exclusive
(例如通过touch
)。