如何使用 npm dbus-network-manager 更改以太网接口的 IP 地址?
Posted
技术标签:
【中文标题】如何使用 npm dbus-network-manager 更改以太网接口的 IP 地址?【英文标题】:how can npm dbus-network-manager be used to change the IP address of an ethernet interface? 【发布时间】:2019-03-22 18:40:29 【问题描述】:我需要使用 DBus 更改网络接口的 v4 IP 地址,和/或将其设置为使用基于 nodejs 的服务的 DHCP。
我已经花了一个多下午的时间尝试使用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 是 1.12.2-1ubuntu1 版本
提前致谢!
【问题讨论】:
我当然应该提到代码无法更改接口的 IP 地址(来自 192.168.1.103),并且我检查了 ...200 地址是否可用。 【参考方案1】:我没有使用 NPM dbus 管理器,但遇到了类似的问题。
更新设置对象后,我还必须Reapply 设备,这样它才能读取这些更改。我还可以使用命令nmcli connection up id eth0
重新加载界面设置。
例子:
conn.Update2(new_settings,2);
dev.Reapply(,0,0)
【讨论】:
以上是关于如何使用 npm dbus-network-manager 更改以太网接口的 IP 地址?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Windows 上使用单个 NPM 命令运行多个 NPM 命令