我试图在 python 中询问 dbus,让 NetworkManager 告诉我连接是否配置为尝试自动连接到网络。
当我使用 nmcli 检查 NetworkManager 连接时,我得到如下输出
connection.id: my_wifi
connection.uuid: f36e1220-cf36-4254-b790-6d21b70ff76a
connection.stable-id: --
connection.type: 802-11-wireless
connection.interface-name: wlan0
connection.autoconnect: yes
connection.autoconnect-priority: 1
connection.autoconnect-retries: -1 (default)
connection.multi-connect: 0 (default)
connection.auth-retries: -1
connection.timestamp: 1690711294
connection.read-only: no
connection.permissions: --
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1 (default)
connection.secondaries: --
connection.gateway-ping-timeout: 0
connection.metered: unknown
connection.lldp: default
connection.mdns: -1 (default)
connection.llmnr: -1 (default)
connection.wait-device-timeout: -1
...
我们可以看到其中显示了connection.autoconnect参数。
但是,如果我尝试使用连接对象上的 GetSettings 方法从 PyQt 程序获取相同的信息,我会得到:
connection:
id:
my_wifi
interface-name:
wlan0
permissions:
<Empty Array>
timestamp:
1689966080
type:
802-11-wireless
uuid:
f36e1220-cf36-4254-b790-6d21b70ff76a
802-11-wireless:
....
这里是查询dbus的代码源码。 (请注意,如果没有格式化代码,我得到的相同结果的可读性较差,因此这不是人为的。)
#!/usr/bin/env python3
# The code below is released under a public domain licence
from dbus import SystemBus, Interface
from dbus.types import Dictionary, String, Array, ByteArray, Byte
# Output formatting code
def display_data(structure, indent: int = 0):
prefix = ""
for i in range (indent):
prefix += " "
if type(structure) == Dictionary:
if len(structure) == 0:
print(f"{prefix}<Empty Dict>")
else:
for element in structure:
if type(structure[element] == Dictionary):
print(f"{prefix}{element}:")
display_data(structure[element], indent + 1)
else:
display_data("structure[element]")
elif type(structure) == Array:
if len(structure) == 0:
print(f"{prefix}<Empty Array>")
elif type(structure[0]) == Byte:
print(f"{prefix}{''.join([str(c) for c in structure])}")
else:
for array_element in structure:
print(f"{prefix}{array_element}")
else:
if type(structure) == String and len(structure) == 0:
print(f"{prefix}<Empty Value>")
print(f"{prefix}{structure}")
# Dbus probe code
interface_netman = "org.freedesktop.NetworkManager"
path_netman_settings = "/org/freedesktop/NetworkManager/Settings"
interface_settings = "org.freedesktop.NetworkManager.Settings"
interface_connection = "org.freedesktop.NetworkManager.Settings.Connection"
bus = SystemBus()
settings_proxy = bus.get_object(interface_netman, path_netman_settings)
settings = Interface(settings_proxy, interface_settings)
connection_list = settings.ListConnections()
out = {}
for connection in connection_list:
this_connection = bus.get_object(interface_netman, connection)
this_connection_interface = Interface(this_connection, interface_connection)
this_connection_settings = this_connection_interface.GetSettings()
display_data(this_connection_settings)
我预计当我运行上面的代码时,我会得到与 nmcli 输出相同的信息,包括连接是否启用了自动连接,但提供的信息要有限得多。
所以我的问题是 nmcli 从哪里获取额外的连接信息,以及如何访问它?
事实证明,GetSettings() 不会为connection.autoconnect 返回值,除非将其设置为非默认值(即“no”)。