Starting tests with npm, nmcli basics (to be extended on linux machine)
This commit is contained in:
parent
5e09da21e4
commit
cc7b23aec1
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
||||
.idea
|
||||
node-modules
|
||||
node_modules
|
||||
|
48
lib/nmcli.js
48
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
|
||||
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
|
||||
};
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
26
test/nmcli.js
Normal file
26
test/nmcli.js
Normal 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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user