这篇文章有两个问题。 1.特定网络设备上的网络接口上/下通知 2.列出网络设备。
请参阅下文,了解我提供的答案。 (感谢Velkan帮助我实现这个)
我正在寻找一个c ++库,它在Ubuntu机器上接收带有接口名称的网络接口上/下事件通知。我的同事们已经要求我使用dbus接口。使用下面的链接相同,只是通知没有接口名称的任何接口up / down事件。
我正在谷歌搜索一段时间,我找不到正确的例子,甚至dbus文档也不是直截了当或不提供我正在寻找的信息。
任何有关这方面的帮助都会非常有帮助。
先谢谢
答案 0 :(得分:0)
我看了d-feet
实用程序。
在'系统总线' /org/freedesktop/NetworkManager
信号为DeviceAdded
。
/org/freedesktop/NetworkManager/Devices/1
事情有:
Interface
org.freedesktop.NetworkManager.Device
中的字符串
{li> Carrier
布尔和PropertiesChanged
信号位于org.freedesktop.NetworkManager.Device.Wired
,org.freedesktop.NetworkManager.Device.Bridge
,org.freedesktop.NetworkManager.Device.Adsl
......
因此,获取NetworkManager对象,收听设备列表的修改,订阅将出现的当前设备和设备的PropertiesChanged
,注意Carrier
属性。
如果要检测接口已启动但未分配IP(或者可能处于其他某种中间状态),请查看其他属性。
如果你正在使用Ubuntu的服务器版本(你不是),那么我认为它没有使用NetworkManager,因此它的D-Bus接口可用性稍差 - { {1}}。
DeviceAdded / DeviceRemoved的Shell示例:
org.freedesktop.network1
插入Wi-Fi USB密钥时:
$ dbus-monitor --system "type='signal',interface='org.freedesktop.NetworkManager'"
发射了几个信号。因此,如果我们订阅signal time=1509615532.227067 sender=:1.8 -> destination=(null destination) serial=2051 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=DeviceAdded
object path "/org/freedesktop/NetworkManager/Devices/10"
signal time=1509615532.228150 sender=:1.8 -> destination=(null destination) serial=2056 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
array [
dict entry(
string "AllDevices"
variant array [
object path "/org/freedesktop/NetworkManager/Devices/0"
object path "/org/freedesktop/NetworkManager/Devices/1"
object path "/org/freedesktop/NetworkManager/Devices/2"
object path "/org/freedesktop/NetworkManager/Devices/3"
object path "/org/freedesktop/NetworkManager/Devices/4"
object path "/org/freedesktop/NetworkManager/Devices/5"
object path "/org/freedesktop/NetworkManager/Devices/6"
object path "/org/freedesktop/NetworkManager/Devices/8"
object path "/org/freedesktop/NetworkManager/Devices/10"
]
)
dict entry(
string "Devices"
variant array [
object path "/org/freedesktop/NetworkManager/Devices/0"
object path "/org/freedesktop/NetworkManager/Devices/1"
object path "/org/freedesktop/NetworkManager/Devices/2"
object path "/org/freedesktop/NetworkManager/Devices/3"
object path "/org/freedesktop/NetworkManager/Devices/4"
object path "/org/freedesktop/NetworkManager/Devices/5"
object path "/org/freedesktop/NetworkManager/Devices/6"
object path "/org/freedesktop/NetworkManager/Devices/8"
object path "/org/freedesktop/NetworkManager/Devices/10"
]
)
]
signal time=1509615533.253596 sender=:1.8 -> destination=(null destination) serial=2072 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
array [
dict entry(
string "WirelessEnabled"
variant boolean false
)
]
signal time=1509615533.287235 sender=:1.8 -> destination=(null destination) serial=2080 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
array [
dict entry(
string "ActiveConnections"
variant array [
object path "/org/freedesktop/NetworkManager/ActiveConnection/3"
object path "/org/freedesktop/NetworkManager/ActiveConnection/2"
object path "/org/freedesktop/NetworkManager/ActiveConnection/1"
object path "/org/freedesktop/NetworkManager/ActiveConnection/0"
]
)
]
或DeviceAdded
,我们可以推断出我们也需要观看设备#10。
当它被删除时:
PropertiesChanged
相同的信号:#10已经消失。
或订阅signal time=1509615612.892305 sender=:1.8 -> destination=(null destination) serial=2086 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=DeviceRemoved
object path "/org/freedesktop/NetworkManager/Devices/10"
signal time=1509615612.893697 sender=:1.8 -> destination=(null destination) serial=2089 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
array [
dict entry(
string "ActiveConnections"
variant array [
object path "/org/freedesktop/NetworkManager/ActiveConnection/3"
object path "/org/freedesktop/NetworkManager/ActiveConnection/2"
object path "/org/freedesktop/NetworkManager/ActiveConnection/1"
object path "/org/freedesktop/NetworkManager/ActiveConnection/0"
]
)
dict entry(
string "AllDevices"
variant array [
object path "/org/freedesktop/NetworkManager/Devices/0"
object path "/org/freedesktop/NetworkManager/Devices/1"
object path "/org/freedesktop/NetworkManager/Devices/2"
object path "/org/freedesktop/NetworkManager/Devices/3"
object path "/org/freedesktop/NetworkManager/Devices/4"
object path "/org/freedesktop/NetworkManager/Devices/5"
object path "/org/freedesktop/NetworkManager/Devices/6"
object path "/org/freedesktop/NetworkManager/Devices/8"
]
)
dict entry(
string "Devices"
variant array [
object path "/org/freedesktop/NetworkManager/Devices/0"
object path "/org/freedesktop/NetworkManager/Devices/1"
object path "/org/freedesktop/NetworkManager/Devices/2"
object path "/org/freedesktop/NetworkManager/Devices/3"
object path "/org/freedesktop/NetworkManager/Devices/4"
object path "/org/freedesktop/NetworkManager/Devices/5"
object path "/org/freedesktop/NetworkManager/Devices/6"
object path "/org/freedesktop/NetworkManager/Devices/8"
]
)
]
signal time=1509615612.910829 sender=:1.8 -> destination=(null destination) serial=2095 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
array [
dict entry(
string "WirelessEnabled"
variant boolean true
)
]
和DeviceAdded
仅获取它们:
DeviceRemoved
答案 1 :(得分:0)
任何想要工作代码示例的人都可以在问题(https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/C/glib/monitor-nm-state-gdbus.c)中共享的链接中替换下面给出的类似行。
在特定设备上收听/关闭事件的代码段 这里我刚刚将对象路径parm值更改为
/组织/的freedesktop /网络管理器/设备/ 1
proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, 旗帜, NULL,/ * GDBusInterfaceInfo / " org.freedesktop.NetworkManager&#34 ;, 的 强> 的" /组织/的freedesktop /网络管理器/设备/ 1" 强> , " org.freedesktop.NetworkManager.Device&#34 ;, NULL,/ GCancellable * / &安培;错误);
用于列出可用设备的代码段。
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
flags,
NULL, /* GDBusInterfaceInfo */
"org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
NULL, /* GCancellable */
&error);
ret = g_dbus_proxy_call_sync (proxy,
"GetDevices",
NULL,
G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &error);
if (!ret) {
g_dbus_error_strip_remote_error (error);
g_print ("ListConnections failed: %s\n", error->message);
g_error_free (error);
return;
}
g_variant_get (ret, "(^ao)", &paths);
g_variant_unref (ret);
for (i = 0; paths[i]; i++)
g_print ("%s\n", paths[i]);
网络设备列表的输出(我的机器中有2张nic卡)
/组织/的freedesktop /网络管理器/设备/ 0
/组织/的freedesktop /网络管理器/设备/ 1