使用node.js控制wpa_supplicant

使用node.js控制wpa_supplicant

我想使用 node.js 模組控制 wpa_supplicant 。最重要的是,我希望能夠偵測連線故障,以便我可以編寫一個可以對它們採取行動的程式。

到目前為止,使用 wpa_supplicant 和終端命令設定無線連線已經成功。我嘗試使用 dbus-native 模組存取 wlan0 接口,但無法存取該接口。

如果我能指出正確的方向,我也願意使用其他現有的節點模組或編寫自己的節點模組。

有人可以幫助我在這裡進步嗎?

到目前為止我嘗試過的程式碼:

var dbus = require('dbus-native');
var util = require('util');

var bus = dbus.systemBus();
var wpas = bus.getService('fi.w1.wpa_supplicant1');

var wpai = wpas.getInterface('/fi/w1/wpa_supplicant1'
    , 'fi.w1.wpa_supplicant1', function (err, iface) {
        //console.log(err, iface);

        iface.on('PropertiesChanged', function(dict) {
            console.log('interface properties have changed!');
            console.log(dict);
        });

        iface.on('InterfaceAdded', function(path, dict) {
            console.log('interface has been added!');
            console.log(path, dict);
        });

        iface.on('InterfaceRemoved', function(path) {
            console.log('interface has been removed!');
            console.log(path);
        });

        // wpa_supplicant denies knowledge of the interface
        iface.GetInterface('wlan0', function (err, iface2) {
            console.log( arguments );
            console.log(err, iface2);
        });

        //error couldn't grab interface
        iface.CreateInterface([
            ['Ifname',
                ['s', 'wlan0']
            ]
        ], function (err, iface3){
            console.log(err, iface3);
        });

        //error couldn't grab interface
        iface.CreateInterface([
            ['Ifname',
                ['s', 'wlan0']
            ],
            ['Driver',
                ['s', 'nl80211']
            ],
            ['ConfigFile',
                ['s', '~/etc/wpa_supplicant/wpa_supplicant.conf']
            ]
        ], function (err, iface3){
            console.log(err, iface3);
        });

    });

更新1:

我使用 DBus 屬性 api 來調查 Interfaces 屬性,發現該屬性本身為 null。

wpas.getInterface('/fi/w1/wpa_supplicant1', 'org.freedesktop.DBus.Properties', function(err, device) {
                device.GetAll('fi.w1.wpa_supplicant1', function(err, prop) {
                    var props = arrToMap(prop);
                    console.log(err,props);
                });
            });

function arrToMap(arr) {
    var output = {};
    for (var i = 0; i < arr.length; i++) {
        output[arr[i][0]] = arr[i][1][1][0];
    }
    return output;
}

我唯一的結論是 wpa_supplicant 從未向 dbus 註冊任何新介面。

(我已確保使用終端命令使用 wpa_supplicant 設定了 wlan0)

更新2:

我一直試圖找出為什麼我的程式碼的以下部分不斷給我錯誤:

[“wpa_supplicant 無法取得此介面。” ]

  iface.CreateInterface([
            ['Ifname',
                ['s', 'wlan0']
            ]
        ], function (err, iface3){
            console.log(err, iface3);
        });

答案1

不確定 NodeJs 如何處理這個問題,但無法在 wpa 請求者上一次又一次地建立介面。一旦你在 wpa 上註冊了一個接口,它就會在再次呼叫“create_interface”時拋出錯誤。

澄清一下,「建立介面」是一項僅在您第一次使用該介面時使用的功能。例如,我在 python 中使用過 wpa 請求者,並且 python wpa 請求者 API 有兩個功能,「create_interface」和「get_interface」。

我一直在 IoT 設備上使用它,所以分享一下 Python 程式碼:

try:
    wpa.create_interface('wlp2s0')
except:
    wpa.get_interface('wlp2s0')

其中“wlp2s0”是介面名稱。

相關內容