Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse iwlist scan output in JavaScript (nodejs)
function iwlistParse(str) {
var out = str.replace(/^\s+/mg, '');
out = out.split('\n');
var cells = [];
var line;
var info = {};
var fields = {
'mac' : /^Cell \d+ - Address: (.*)/,
'ssid' : /^ESSID:"(.*)"/,
'protocol' : /^Protocol:(.*)/,
'mode' : /^Mode:(.*)/,
'frequency' : /^Frequency:(.*)/,
'encryption_key' : /Encryption key:(.*)/,
'bitrates' : /Bit Rates:(.*)/,
'quality' : /Quality(?:=|\:)([^\s]+)/,
'signal_level' : /Signal level(?:=|\:)([^\s]+)/
};
for (var i=0,l=out.length; i<l; i++) {
line = out[i].trim();
if (!line.length) {
continue;
}
if (line.match("Scan completed :$")) {
continue;
}
if (line.match("Interface doesn't support scanning.$")) {
continue;
}
if (line.match(fields.mac)) {
cells.push(info);
info = {};
}
for (var field in fields) {
if (line.match(fields[field])) {
info[field] = (fields[field].exec(line)[1]).trim();
}
}
}
cells.push(info);
return cells;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.