diff --git a/.gitignore b/.gitignore index 75868ca..7a1537b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .idea -node-modules +node_modules diff --git a/lib/nmcli.js b/lib/nmcli.js index 9f2feec..f0ac446 100644 --- a/lib/nmcli.js +++ b/lib/nmcli.js @@ -1,5 +1,51 @@ /** + * Scanning WiFis on Mac OS X using nmcli * Created by kc on 04.04.16. */ -// nmcli -m tabular -f SSID,BSSID,SIGNAL,FREQ device wifi \ No newline at end of file +const _ = require('lodash'); +const tool = '/usr/bin/nmcli'; +const cmdLine = tool + ' -m tabular -f SSID,BSSID,SIGNAL,FREQ device wifi'; + +const macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/; +/** + * Parsing the output of nmcli + * @param str output of the tool + * @param callback + */ +function parseOutput(str, callback) { + var err = null; + + try { + var lines = str.split('\n'); + var wifis = []; + + for (var i = 1, l = lines.length; i < l; i++) { + var mac = lines[i].match(macRegex); + if (!mac) { + continue; + } + var macStart = lines[i].indexOf(mac[0]); + var elements = lines[i].substr(macStart).split(/[ ]+/); + wifis.push({ + 'ssid' : _.trim(lines[i].substr(0, macStart), ' \''), + 'mac' : elements[0].trim(), + 'channel' : parseInt(elements[2].trim(), 10), + 'rssi' : parseInt(elements[1].trim()), + 'security': 'TODO' + }); + } + } + catch (ex) { + err = ex; + } + + callback(err, wifis); +} + + +module.exports = { + parseOutput: parseOutput, + cmdLine : cmdLine, + tool : tool +}; diff --git a/package.json b/package.json index 2b68822..8019ea3 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "url": "http://www.kusti.ch/" }, "homepage": "http://www.ferropoly.ch/", + "license": "MIT", "repository": { "type": "git", "url": "https://github.com/ancasicolica/node-wifi-scanner.git" @@ -21,5 +22,11 @@ "engines": { "node": ">= 4.4.0", "npm": ">= 2.14.0" + }, + "scripts": { + "test": "mocha test" + }, + "dependencies": { + "lodash": "4.8.1" } } diff --git a/test/airport.js b/test/airport.js index 689810a..8c0cdfc 100644 --- a/test/airport.js +++ b/test/airport.js @@ -46,7 +46,6 @@ describe('airport', () => { it('parses the output of file 2', function(done) { airport.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','airport','airport02.txt'), { encoding: 'utf8' }), (err, info) => { - console.log(info); assert.ok(info); assert.equal(info.length, 4); diff --git a/test/nmcli.js b/test/nmcli.js new file mode 100644 index 0000000..0bac82f --- /dev/null +++ b/test/nmcli.js @@ -0,0 +1,26 @@ +/** + * nmcli unit test + * Created by kc on 04.04.16. + */ + +const fs = require('fs'); +const path = require('path'); +const assert = require('assert'); + +const nmcli = require('../lib/nmcli'); + +describe.skip('nmcli', () => { + it('parses the output of file 1', function(done) { + nmcli.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','nmcli','nmcli01.txt'), { encoding: 'utf8' }), (err, info) => { + + console.log(info); + assert.ok(info); + assert.equal(info.length, 36); + + + + done(err); + }); + }); + +}); \ No newline at end of file