Created
December 29, 2012 17:19
-
-
Save mauricesvay/4408150 to your computer and use it in GitHub Desktop.
Parse iwlist scan output in JavaScript (nodejs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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