我想根据一些外部逻辑切换物联网设备连接到哪个AP。我的 AP 切换脚本如下所示:
function switch_ap() {
current_connection=$(nmcli conn show --active | tail -n 1 | cut -d' ' -f1)
nmcli conn down $current_connection
if test "$current_connection" = "home_ap"; then
nmcli conn up iot_ap
else
nmcli conn up home_ap
fi
new_connection=$(nmcli conn show --active | tail -n 1 | cut -d' ' -f1)
echo "was connected to $current_connection, now connected to connection: $(nmcli conn show --active | tail -n 1 | cut -d' ' -f1) ssid: $(iwgetid wlan0 -r)"
}
有时这有效,但不可靠。更多时候我会得到一个错误:
Connection 'home_ap' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/66)
Error: Connection activation failed: The base network connection was interrupted
Hint: use 'journalctl -xe NM_CONNECTION=dcbf7f18-1e26-41bb-a874-c1ce6718dbf2 + NM_DEVICE=wlan0' to get more details.
我没有看到运行journalctl
命令的问题,但我将在本文底部分享输出,以防其他人更擅长解析这些日志。
我的黑客修复是这个功能:
function switch_ap() {
current_connection=$(nmcli conn show --active | tail -n 1 | cut -d' ' -f1)
nmcli conn down $current_connection
if test "$current_connection" = "home_ap"; then
mv /etc/nm_connections/iot_ap.nmconnection /opt/nm_connections/
mv /opt/home_ap.nmconnection /etc/nm_connections/
nmcli conn up iot_ap
else
mv /etc/nm_connections/home_ap.nmconnection /opt/nm_connections/
mv /opt/nm_connections/iot_ap.nmconnection /etc/nm_connections/
nmcli conn up home_ap
fi
new_connection=$(nmcli conn show --active | tail -n 1 | cut -d' ' -f1)
echo "was connected to $current_connection, now connected to connection: $(nmcli conn show --active | tail -n 1 | cut -d' ' -f1) ssid: $(iwgetid wlan0 -r)"
}
这个似乎工作可靠,但现在如果我失去与所连接的应用程序的连接,我不会自动切换,这是一种黑客行为。我现在也更频繁地看到这些警告:
(process:12734): GLib-GIO-WARNING **: 06:05:20.284: ../../gio/gdbusobjectmanagerclient.c:1589: Processing InterfaceRemoved signal for path /org/freedesktop/NetworkManager/IP4Config/35 but no object proxy exists
(process:12734): GLib-GIO-WARNING **: 06:05:20.293: ../../gio/gdbusobjectmanagerclient.c:1589: Processing InterfaceRemoved signal for path /org/freedesktop/NetworkManager/IP6Config/35 but no object proxy exists
(process:12734): GLib-GIO-WARNING **: 06:05:20.591: ../../gio/gdbusobjectmanagerclient.c:1589: Processing InterfaceRemoved signal for path /org/freedesktop/NetworkManager/ActiveConnection/35 but no object proxy exists
有什么办法可以让我的 AP 切换更可靠吗?
以下是命令的日志journalctl
:
Connection 'iot_ap' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/33)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/37)
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.5536] device (wlan0): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.5912] device (wlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.5946] device (wlan0): Activation: (wifi) access point 'home_ap' has security, but secrets are required.
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.5948] device (wlan0): state change: config -> need-auth (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.6554] device (wlan0): state change: need-auth -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.6598] device (wlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.6634] device (wlan0): Activation: (wifi) connection 'home_ap' has security, and secrets exist. No new secrets needed.
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.9623] device (wlan0): disconnecting for new activation request.
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.9626] device (wlan0): state change: config -> deactivating (reason 'new-activation', sys-iface-state: 'managed')
Apr 21 09:03:08 DEVICE-072059 NetworkManager[249]: <info> [1587481388.9781] device (wlan0): state change: deactivating -> disconnected (reason 'new-activation', sys-iface-state: 'managed')
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.0102] device (wlan0): Activation: starting connection 'iot_ap' (b109024a-84b9-46b7-a810-2d2472676c7f)
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.0413] device (wlan0): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.0750] device (wlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.0810] device (wlan0): Activation: (wifi) access point 'iot_ap' has security, but secrets are required.
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.0819] device (wlan0): state change: config -> need-auth (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.0867] sup-iface[0x3a2201c0,wlan0]: wps: type pbc start...
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.8388] device (wlan0): state change: need-auth -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.8922] device (wlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.9060] device (wlan0): Activation: (wifi) connection 'iot_ap' has security, and secrets exist. No new secrets needed.
Apr 21 09:03:09 DEVICE-072059 NetworkManager[249]: <info> [1587481389.9470] device (wlan0): supplicant interface state: disconnected -> inactive
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.4927] device (wlan0): supplicant interface state: inactive -> associating
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.5582] device (wlan0): disconnecting for new activation request.
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.5584] device (wlan0): state change: config -> deactivating (reason 'new-activation', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.5645] device (wlan0): state change: deactivating -> disconnected (reason 'new-activation', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.5915] device (wlan0): Activation: starting connection 'home_ap' (dcbf7f18-1e26-41bb-a874-c1ce6718dbf2)
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.6027] device (wlan0): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.6214] device (wlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.6386] device (wlan0): Activation: (wifi) access point 'home_ap' has security, but secrets are required.
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.6388] device (wlan0): state change: config -> need-auth (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <warn> [1587481390.6968] sup-iface[0x3a2201c0,wlan0]: connection disconnected (reason -3)
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.6974] device (wlan0): supplicant interface state: associating -> disconnected
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.6983] device (wlan0): state change: need-auth -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.7046] device (wlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.7357] device (wlan0): Activation: (wifi) connection 'home_ap' has security, and secrets exist. No new secrets needed.
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <info> [1587481390.7815] device (wlan0): supplicant interface state: disconnected -> associated
Apr 21 09:03:10 DEVICE-072059 NetworkManager[249]: <warn> [1587481390.7900] sup-iface[0x3a2201c0,wlan0]: connection disconnected (reason -3)
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.0013] device (wlan0): supplicant interface state: associated -> disconnected
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.6999] device (wlan0): supplicant interface state: disconnected -> associating
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.7886] device (wlan0): supplicant interface state: associating -> 4-way handshake
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.8417] device (wlan0): supplicant interface state: 4-way handshake -> completed
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.8419] device (wlan0): Activation: (wifi) Stage 2 of 5 (Device Configure) successful. Connected to wireless network "TheHome"
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.8624] device (wlan0): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.8689] dhcp4 (wlan0): activation: beginning transaction (timeout in 45 seconds)
Apr 21 09:03:11 DEVICE-072059 NetworkManager[249]: <info> [1587481391.8964] dhcp4 (wlan0): dhclient started with pid 22677
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2540] dhcp4 (wlan0): address 192.168.1.61
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2542] dhcp4 (wlan0): plen 24 (255.255.255.0)
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2543] dhcp4 (wlan0): gateway 192.168.1.1
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2546] dhcp4 (wlan0): lease time 10806
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2547] dhcp4 (wlan0): nameserver '75.75.75.75'
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2548] dhcp4 (wlan0): nameserver '75.75.76.76'
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2550] dhcp4 (wlan0): state changed unknown -> bound
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.2964] device (wlan0): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.3029] device (wlan0): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.3051] device (wlan0): state change: secondaries -> activated (reason 'none', sys-iface-state: 'managed')
Apr 21 09:03:12 DEVICE-072059 NetworkManager[249]: <info> [1587481392.4438] device (wlan0): Activation: successful, device activated.
答案1
网络管理器用于管理网络,它具有根据可用性切换网络的逻辑。当您运行第一个脚本并将其删除时,网络管理器守护进程可能会注意到并连接到可用的默认值。
第二个是移动告诉网络管理员应该连接到哪些 wifi 接入点的文件。因此,当它拥有主文件时,它只能连接到主目录,因为这是它知道的唯一网络。当它有 iot 文件时,它只能连接到 iot。
它不会切换,因为网络管理员不知道另一个存在。解决问题的最佳方法是在网络管理器中设置 SSID 的优先级,或者使用“nmcli device wifi connect”,它应该可以连接到不同的 SSID,而无需将其关闭。如果没有该命令, nmcli 手册页应该有助于了解如何操作。