Skip to content

Instantly share code, notes, and snippets.

@mauricesvay
Created December 29, 2012 17:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricesvay/4408150 to your computer and use it in GitHub Desktop.
Save mauricesvay/4408150 to your computer and use it in GitHub Desktop.
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