Starting tests with npm, nmcli basics (to be extended on linux machine)

This commit is contained in:
Christian Kuster 2016-04-04 21:06:01 +02:00
parent 5e09da21e4
commit cc7b23aec1
5 changed files with 81 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
.idea .idea
node-modules node_modules

View File

@ -1,5 +1,51 @@
/** /**
* Scanning WiFis on Mac OS X using nmcli
* Created by kc on 04.04.16. * Created by kc on 04.04.16.
*/ */
// nmcli -m tabular -f SSID,BSSID,SIGNAL,FREQ device wifi 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
};

View File

@ -14,6 +14,7 @@
"url": "http://www.kusti.ch/" "url": "http://www.kusti.ch/"
}, },
"homepage": "http://www.ferropoly.ch/", "homepage": "http://www.ferropoly.ch/",
"license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/ancasicolica/node-wifi-scanner.git" "url": "https://github.com/ancasicolica/node-wifi-scanner.git"
@ -21,5 +22,11 @@
"engines": { "engines": {
"node": ">= 4.4.0", "node": ">= 4.4.0",
"npm": ">= 2.14.0" "npm": ">= 2.14.0"
},
"scripts": {
"test": "mocha test"
},
"dependencies": {
"lodash": "4.8.1"
} }
} }

View File

@ -46,7 +46,6 @@ describe('airport', () => {
it('parses the output of file 2', function(done) { it('parses the output of file 2', function(done) {
airport.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','airport','airport02.txt'), { encoding: 'utf8' }), (err, info) => { airport.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','airport','airport02.txt'), { encoding: 'utf8' }), (err, info) => {
console.log(info);
assert.ok(info); assert.ok(info);
assert.equal(info.length, 4); assert.equal(info.length, 4);

26
test/nmcli.js Normal file
View File

@ -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);
});
});
});