我正在尝试选择其他SSID来从浏览器切换您连接的Wlan。

var sys = require('sys');
var exec = require('child_process').exec;
app.get(prefix + '/wlan', function(req, res){
child = exec("iwlist wlan0 scan | grep ESSID", function(error, stdout, stderr){
                if(error !== null){
                    console.log('Exec error ' + error);
                }
                else {
                    res.send(stdout);
                }
 });
});


到目前为止,这是我的代码以获取SSID列表。

输出是这样的:

ESSID:"WLAN-GUEST" ESSID:"WLAN1" ESSID:"WLAN-GUEST" ESSID:"WLAN1" ESSID:"WLAN2"

I have no idea why two ESSID's are listed twice but my main question is, how can I parse this to JSON or how can I access each entry like an array (wlanlist[0])?

Edit:I tried to stdout.replace(" ",", "); and JSON.parse but as it's async it's sent without changes. (Not sure if that would work as sync)

Edit2: Trying to access the data like that:

$(document).ready(function() {
$.get(prefix + '/wlan', function(wlanlist){

    document.getElementById("wlanoptions").options[0] = new Option("Select your WLAN:","");
    document.getElementById("wlanoptions").options[1] = new Option(wlanlist[0],wlanlist[0])
});
});


最后结果:

var wlanlistarray = stdout.split("ESSID:");res.send(wlanlistarray);

此外:

//extract ssid and remove quotes
                var wlanlist = new Array;
                var step1 = stdout.split("ESSID:");
                for(i = 1; i < step1.length; i++){
                    var arr = new Array;
                    arr = step1[i].split('"');
                    //if exists in array -> continue; else create new entry in wlanlist
                    if(wlanlist.indexOf(arr[1]) === -1){wlanlist.push(arr[1]);}
                    else{continue;}
                }
                res.send(wlanlist);

最佳答案

这应该返回一个SSID数组:

stdout.split("ESSID:")


现在清理",您已完成。

07-24 09:44
查看更多