diff --git a/lib/iwlist.js b/lib/iwlist.js index a1398a1..b4e5780 100644 --- a/lib/iwlist.js +++ b/lib/iwlist.js @@ -1,3 +1,84 @@ /** + * iwlist - a powerful tool with a horrible output * Created by kc on 04.04.16. */ + +const _ = require('lodash'); +const tool = '/usr/bin/iwlist'; +const cmdLine = tool + ' scan'; + +const macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/; +const cellRegex = /Cell [0-9]{2,} - Address:/; +/** + * Parsing the output of iwlist, tool having a lot of different faces :-( + * @param str output of the tool + * @param callback + */ +function parseOutput(str, callback) { + var err = null; + var wifis = []; + + try { + var blocks = str.split(cellRegex); + + blocks.forEach(block => { + var network = {}; + var lines = block.split('\n'); + if (macRegex.exec(lines[0])) { + // First line is the mac address (always! (?)) + network.mac = lines[0].trim(); + + lines.forEach(line => { + // SSID + if (line.indexOf('ESSID:') > 0) { + network.ssid = _.trim(line.split(':')[1], '"'); + } + + // Security + else if (_.startsWith(line.trim(), 'IE:')) { + network.security = line.split(':')[1].trim(); + } + + // Channel, an ugly thing to get it + else if (_.startsWith(line.trim(), 'Frequency:')) { + network.channel = parseInt(_.trim(line, ' )').split(/Channel/)[1], 10); + } + + // Another ugly thing, the signal which can have different formats, even worse als + // having different identifiers + else if (line.indexOf('Signal level') > -1) { + if (line.indexOf('Quality') > -1) { + // This is a "Quality=40/70 Signal level=-70 dBm" line + network.rssi = parseInt(line.substr(line.indexOf('Signal level') + 13), 10); + } + else { + // This is a "Signal level=60/100" line + var elements = line.split('='); + elements.forEach(e => { + if (e.indexOf('/') > 0) { + // that's our part + var parts = e.split('/'); + var level = Math.floor(100 * parseInt(parts[0], 10) / parseInt(parts[1], 10)); + network.rssi = level / 2 - 100; + } + }) + } + } + }); + wifis.push(network); + } + }); + } + catch (ex) { + err = ex; + } + + callback(err, wifis); +} + + +module.exports = { + parseOutput: parseOutput, + cmdLine : cmdLine, + tool : tool +}; diff --git a/test/fixtures/iwlist/iwlist01.txt b/test/fixtures/iwlist/iwlist01_ubuntu1404.txt similarity index 100% rename from test/fixtures/iwlist/iwlist01.txt rename to test/fixtures/iwlist/iwlist01_ubuntu1404.txt diff --git a/test/fixtures/iwlist/iwlist02_raspi.txt b/test/fixtures/iwlist/iwlist02_raspi.txt new file mode 100644 index 0000000..59a3899 --- /dev/null +++ b/test/fixtures/iwlist/iwlist02_raspi.txt @@ -0,0 +1,97 @@ +wlan0 Scan completed : + Cell 01 - Address: D4:D1:84:50:76:45 + Channel:6 + Frequency:2.437 GHz (Channel 6) + Quality=34/70 Signal level=-76 dBm + Encryption key:on + ESSID:"gsy-97796" + Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s + 24 Mb/s; 36 Mb/s; 54 Mb/s + Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s + Mode:Master + Extra:tsf=0000000000000000 + Extra: Last beacon: 17180ms ago + IE: Unknown: 00096773792D3937373936 + IE: Unknown: 010882848B962430486C + IE: Unknown: 030106 + IE: Unknown: 0706434820010D14 + IE: Unknown: 2A0100 + IE: Unknown: 2F0100 + IE: IEEE 802.11i/WPA2 Version 1 + Group Cipher : TKIP + Pairwise Ciphers (2) : CCMP TKIP + Authentication Suites (1) : PSK + IE: Unknown: 32040C121860 + IE: Unknown: 2D1A7C181BFFFF000000000000000000000000000000000000000000 + IE: Unknown: 3D1606080000000000000000000000000000000000000000 + IE: Unknown: DD8A0050F204104A00011010440001021041000100103B00010310470010D757BB43C912588590477819BAC0ADCF1021000B506972656C6C692042425310230007563232364E31571024000D333336303249303036383835381042000D333336303249303036383835381054000800060013C804000110110007563232364E3157100800020080103C000101 + IE: Unknown: DD090010180200F02C0000 + IE: WPA Version 1 + Group Cipher : TKIP + Pairwise Ciphers (2) : CCMP TKIP + Authentication Suites (1) : PSK + IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00 + Cell 02 - Address: 7C:B7:33:AE:3B:04 + Channel:9 + Frequency:2.452 GHz (Channel 9) + Quality=36/70 Signal level=-74 dBm + Encryption key:on + ESSID:"Raupo" + Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s + 24 Mb/s; 36 Mb/s; 54 Mb/s + Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s + Mode:Master + Extra:tsf=0000000000000000 + Extra: Last beacon: 17180ms ago + IE: Unknown: 0005526175706F + IE: Unknown: 010882848B962430486C + IE: Unknown: 030109 + IE: Unknown: 0706455520010D14 + IE: Unknown: 2A0100 + IE: Unknown: 32040C121860 + IE: IEEE 802.11i/WPA2 Version 1 + Group Cipher : TKIP + Pairwise Ciphers (2) : CCMP TKIP + Authentication Suites (1) : PSK + IE: Unknown: 0B0503000F0000 + IE: Unknown: 46053208010000 + IE: Unknown: 2D1AFE191BFFFF000001000000000000000000000000000000000000 + IE: Unknown: 3D16090F1600000000000000000000000000000000000000 + IE: Unknown: 7F03040008 + IE: Unknown: DD9B0050F204104A0001101044000102103B0001031047001044504F444040FE52F04F3D4144504F441021000953696C6967656E63651023000853472D4E502D303010240005352E302E301042000F352E303134303746304337363330361054000800060050F20400011011001A5377697373636F6D20496E7465726E65742D426F7820706C7573100800020006103C0001031049000600372A000120 + IE: Unknown: DD090010180203000C0000 + IE: WPA Version 1 + Group Cipher : TKIP + Pairwise Ciphers (2) : CCMP TKIP + Authentication Suites (1) : PSK + IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00 + Cell 03 - Address: 7C:B7:33:AE:3B:05 + Channel:9 + Frequency:2.452 GHz (Channel 9) + Quality=40/70 Signal level=-70 dBm + Encryption key:on + ESSID:"visitor-18170" + Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s + 24 Mb/s; 36 Mb/s; 54 Mb/s + Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s + Mode:Master + Extra:tsf=0000000000000000 + Extra: Last beacon: 17180ms ago + IE: Unknown: 000D76697369746F722D3138313730 + IE: Unknown: 010882848B962430486C + IE: Unknown: 030109 + IE: Unknown: 0706455520010D14 + IE: Unknown: 2A0100 + IE: Unknown: 32040C121860 + IE: IEEE 802.11i/WPA2 Version 1 + Group Cipher : CCMP + Pairwise Ciphers (1) : CCMP + Authentication Suites (1) : PSK + IE: Unknown: 0B0500000F0000 + IE: Unknown: 46053208010000 + IE: Unknown: 2D1AFE191BFFFF000001000000000000000000000000000000000000 + IE: Unknown: 3D16090F1600000000000000000000000000000000000000 + IE: Unknown: 7F03040008 + IE: Unknown: DD090010180200000C0000 + IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00 + diff --git a/test/iwlist.js b/test/iwlist.js new file mode 100644 index 0000000..c6247a4 --- /dev/null +++ b/test/iwlist.js @@ -0,0 +1,31 @@ +/** + * iwlist tests + * Created by kc on 05.04.16. + */ + +const fs = require('fs'); +const path = require('path'); +const assert = require('assert'); + +const iwlist = require('../lib/iwlist'); + +describe('iwlist', () => { + it('parses the output of file 1', function(done) { + iwlist.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','iwlist','iwlist01_ubuntu1404.txt'), { encoding: 'utf8' }), (err, info) => { + + console.log(info); + assert.ok(info); + done(err); + }); + }); + + it('parses the output of file 2', function(done) { + iwlist.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','iwlist','iwlist02_raspi.txt'), { encoding: 'utf8' }), (err, info) => { + + console.log(info); + assert.ok(info); + done(err); + }); + }); + +}); \ No newline at end of file