0

I'm trying to make it possible to choose different SSID's to switch the Wlan you are connected to from Browser.

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);
                }
 });
});

This is my code so far to get a SSID list..

The Output is like that:

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])
});
}); 

Final Result:

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

In addition:

//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);
LinkM
  • 119
  • 3
  • 11
  • They are probably listed twice because two routers broadcast the same SSID. Maybe a repeater. – wtfzn Jun 23 '16 at 14:11
  • Ah yes that could be true, thanks. – LinkM Jun 23 '16 at 14:13
  • I am a little confused here. This looks like node.js code and yet you say you are running this in the browser. Are you really running this in a browser? – bhspencer Jun 23 '16 at 14:16
  • also the command you are passing to the exec call, shouldn't that be a String? – bhspencer Jun 23 '16 at 14:18
  • Yes it's node.js code and this is serversided and accessed by ajax requests to create a dynamic Selectbox with SSID's. I'm new to that, not sure if I'm doing this right. And yes it's a string, somehow forgot to copy that. Edited, thanks. – LinkM Jun 23 '16 at 14:19
  • What do you mean by "JSON"? An object? A JSON string? – Ates Goral Jun 23 '16 at 15:04
  • I meant a JSON object to access it like an array but isn't it possible to access a json string like an object? Wouldnt really matter then if it's a string or object i guess.. But as I'm new to this, feel free to explain it to me if I'm wrong, always good to learn more. – LinkM Jun 23 '16 at 15:16

1 Answers1

1

This should return an array of SSIDs:

stdout.split("ESSID:")

Now clean up the " and you are all done.

wtfzn
  • 533
  • 5
  • 13
  • I understand what you want to try and I also think it should work but isn't it stdout.split("ESSID:") and afterwards res.send(stdout); ? But somehow If i try to access it later stdout[0] is empty. Sorry, pretty new to this.. – LinkM Jun 23 '16 at 14:57
  • yes, you are right. stdout.split does the trick. .split splits the string by all occurences of "ESSID:". So the first entry is empty because there is nothing to the left of the first "ESSID". Just inspect the whole array instead of the first item inside the array. – wtfzn Jun 23 '16 at 15:02
  • If I console.log(wlanlist); everything is listed but at every wlanlist[0], wlanlist[1], wlanlist[2] and so on, it's empty. Even with using : as split operator.. Accepting your answer though as I think it's the right answer for usual. Let's see if I can fix that. – LinkM Jun 23 '16 at 15:25