如何使用npm dbus-network-manager来更改以太网接口的IP地址?

问题描述 投票:1回答: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

提前致谢!

javascript node.js dbus networkmanager
1个回答
0
投票

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

更新设置对象后,我还必须使用Reapply设备,以便读取这些更改。我还可以使用命令nmcli connection up id eth0重新加载接口设置。

例:

conn.Update2(new_settings,2);
dev.Reapply({},0,0)
© www.soinside.com 2019 - 2024. All rights reserved.