Networking
NetworkManager 切換 AP 失敗:連接啟動失敗
我想根據一些外部邏輯切換物聯網設備連接到哪個 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
命令的問題,但我會在這篇文章的底部分享輸出,以防其他人更擅長解析這些日誌。我的hack-fix是這個功能:
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)" }
這個似乎工作可靠,但現在如果我失去與連接的 ap 的連接,我不會自動切換,這是一個 hack。我現在也更頻繁地看到這些警告:
(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.
網路管理器用於管理網路,它具有根據可用性切換網路的邏輯。當您執行第一個腳本並將其刪除時,網路管理器守護程序可能會注意到並連接到任何可用的預設值。
第二個是你移動的文件,告訴網路管理員它應該連接到哪些 wifi 接入點。因此,當它擁有 home 文件時,它只能連接到 home,因為這是它知道的唯一網路。當它有 iot 文件時,它只能連接到 iot。
它不會切換,因為網路管理員不知道另一個存在。解決問題的最佳方法是在網路管理器中設置 SSID 的優先級或使用“nmcli device wifi connect”,它應該可以連接到不同的 SSID 而不會關閉它。如果不是該命令,那麼 nmcli 手冊頁應該有助於確定如何操作。