Мне нужно использовать DBus для изменения IP-адреса v4 сетевого интерфейса и / или настроить его на использование DHCP из службы на основе nodejs.
Я потратил больше дня, пытаясь использовать dbus-network-manager с этой целью.Я думаю, что я близко, но сигары еще нет.
Вот мой код клиента, он близок к минимальному, несмотря на его длину, извините.
// convenience logging method.
function pretty(obj) {return JSON.stringify(obj,null,2)};
// connect to NetworkManager via DBus
const NetWorkManager = require('dbus-network-manager').connect()
.then(nm => {
// Get a device inventory.
nm.GetDevices()
.then (devices => {
// filter out the Ethernet device to try to change its address
devices.forEach(dev => {
console.log (dev.objectPath);
dev.getProperties()
.then(props => {
// this is where we filter out non-Ethernet interfaces.
if (props.DeviceType == nm.fromEnum(nm.enums.DeviceType, 'Ethernet')) {
// get the ActiveConnection so we can get at the interface config
nm.ActiveConnection.connect(props.ActiveConnection)
.then(ac => {
//console.log(ac);
return ac.getProperties();
})
.then (acProps => {
//console.log ('ActiveConnection: ' + pretty(acProps));
// get our connection which should let us change the interface settings.
return nm.Connection.connect(acProps.Connection);
})
.then (conn => {
// get the settings, so we can use the object
// as a template to adjust & send back to NM.
conn.GetSettings()
.then (settings => {
// log the settings we read, overwrite what seems sensible
console.log('original: ' + pretty(settings.ipv4));
settings.ipv4['address-data'] = [
{
"address": "192.168.1.200",
"prefix": 24
}
];
// 192.168.1.200, 24, 192.168.1.254 in network order.
settings.ipv4.addresses = [3355551936,24,4261521600];
settings.ipv4.method = 'static';
// this should do the trick (but it doesn't), 2 = write to memory
conn.Update2(settings,2);
console.log ('updated: ' + pretty(settings.ipv4));
// these event handlers are never triggered
conn.on('Updated', () => {
console.log ('settings were updated');
});
conn.on('PropertiesChanged', res => {
console.log ('Properties changed: ', pretty(res));
});
});
});
} else {
console.log ('skipping: ' + props.Interface +
', which is of type: ' + nm.toEnum(nm.enums.DeviceType, props.DeviceType)
)
}
});
});
});
})
.catch (err => {
console.log ('Problem: ' + err);
})
И вот что он записывает ...
skipping: lo, which is of type: Generic
skipping: docker0, which is of type: Bridge
original: {
"method": "auto",
"dns": [],
"dns-search": [],
"addresses": [],
"routes": [],
"address-data": [],
"route-data": []
}
updated: {
"method": "static",
"dns": [],
"dns-search": [],
"addresses": [
3355551936,
24,
4261521600
],
"routes": [],
"address-data": [
{
"address": "192.168.1.200",
"prefix": 24
}
],
"route-data": []
}
NetworkManager - это версия 1.10.6-2ubuntu
Ubuntu - это версия 18.04.1 LTS
Nodejs - это версия 8.9.1
DBus isверсия 1.12.2-1ubuntu1
Заранее спасибо!