我正在尝试使用 libnm 在 C++ 应用程序中更改我的接口的 IP 地址,但我找不到一个很好的例子。现在我能够获取设备处理程序并请求连接。
int main(int argc, char *argv[])
{
NMClient * client;
GError * error = NULL;
const GPtrArray *connections;
int i;
if (!(client = nm_client_new(NULL, &error)))
if (!nm_client_get_nm_running(client)) {
g_message("Error: Can't obtain connections: NetworkManager is not running.");
return EXIT_FAILURE;
}
// Find the device
NMDevice* device = nm_client_get_device_by_iface (client, "eno1");
NMDeviceState state = nm_device_get_state (device);
if(state == NM_DEVICE_STATE_ACTIVATED)
{
NMActiveConnection* connection = nm_device_get_active_connection(device);
NMIPConfig * ipConfig = nm_active_connection_get_ip4_config(connection);
//what now?
}
else
{
const GPtrArray* availableConnections = nm_device_get_available_connections(device);
for (i = 0; i < availableConnections->len; i++)
{
//How do I iterate through available connections, and how can I add an ip address and activate it?
}
}
g_object_unref(client);
return EXIT_SUCCESS;
}
目前尚不清楚我可以用这些连接做什么,我应该如何修改它们(我想设置 dhcp,或者用最少的配置来固定 IP 地址),我应该如何激活连接并存储它。我无法找到一个很好的例子。