1

我需要使用 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

提前致谢!

4

1 回答 1

0

我没有使用 NPM dbus 管理器,但遇到了类似的问题。

更新设置对象后,我还必须重新应用设备,以便读取这些更改。我还能够使用该命令nmcli connection up id eth0重新加载界面设置。

例子:

conn.Update2(new_settings,2);
dev.Reapply({},0,0)
于 2019-03-13T15:33:41.410 回答