15 Commits

8 changed files with 1712 additions and 32 deletions

View File

@ -4,5 +4,5 @@ branches:
- master - master
- develop - develop
node_js: node_js:
- "4" - "10"
- "5" - "11"

View File

@ -13,8 +13,6 @@ The module was inspired from Maurice Sways "[node-wifiscanner](https://github.co
had to handle much more complex network environments and also wanted to be independent of the operating had to handle much more complex network environments and also wanted to be independent of the operating
system language. The adaptions needed would have been too comprehensive for a pull request so I decided to write an own module. system language. The adaptions needed would have been too comprehensive for a pull request so I decided to write an own module.
**The module is currently in BETA testing, changes in functionality and interface are possible. Please report bugs on the projects GitHub page, Thanks!**
## Operating Systems ## Operating Systems
It was tested with the following operating systems: It was tested with the following operating systems:
@ -29,6 +27,12 @@ It was tested with the following operating systems:
## Usage ## Usage
### Command Line
Run the script ```scan``` in the bin folder.
### Code
const scanner = require('node-wifi-scanner'); const scanner = require('node-wifi-scanner');
scanner.scan((err, networks) => { scanner.scan((err, networks) => {
@ -55,8 +59,8 @@ The module uses command line tools for gathering the network information:
* airport on Mac OS-X: `airport -s` * airport on Mac OS-X: `airport -s`
* netsh on Windows: `netsh wlan show networks mode=Bssid` * netsh on Windows: `netsh wlan show networks mode=Bssid`
* iwlist (1st choice) on Linux: `iwlist scan` * iwlist on Linux: `iwlist scan`
* nmcli (fallback only) on Linux: `nmcli -m tabular -f SSID,BSSID,SIGNAL,FREQ device wifi`
Unfortunately, Mac OS-X and Windows use the system language for the output which requires a quite Unfortunately, Mac OS-X and Windows use the system language for the output which requires a quite
generic way of parsing the data. If you experience any troubles, please create a GitHub issue and supply generic way of parsing the data. If you experience any troubles, please create a GitHub issue and supply

View File

@ -3,8 +3,9 @@
* Created by kc on 04.04.16. * Created by kc on 04.04.16.
*/ */
const fs = require('fs');
const exec = require('child_process').exec; const exec = require('child_process').exec;
const async = require('async');
const _ = require('lodash');
// The tools // The tools
const airport = require('./lib/airport'); const airport = require('./lib/airport');
const iwlist = require('./lib/iwlist'); const iwlist = require('./lib/iwlist');
@ -14,25 +15,50 @@ var scanner;
// Initializing the tools // Initializing the tools
function initTools(callback) { function initTools(callback) {
fs.stat(airport.tool, function (err, stats) {
if (stats) {
return callback(null, airport);
}
fs.stat(iwlist.tool, function (err, stats) { // When a command is not found, an error is issued and async would finish. Therefore we pack
if (stats) { // the error into the result and check it later on.
return callback(null, iwlist); async.parallel([
function (cb) {
exec(airport.detector, function (err) {
cb(null, {err: err, scanner: airport}
)
}
);
},
function (cb) {
exec(iwlist.detector, function (err) {
cb(null, {err: err, scanner: iwlist}
)
}
);
},
function (cb) {
exec(netsh.detector, function (err) {
cb(null, {err: err, scanner: netsh}
)
}
);
} }
fs.stat(netsh.tool, function (err, stats) { ],
if (stats) { function (err, results) {
return callback(null, netsh); var res = _.find(results,
} function (f) {
callback(new Error('No scanner found')); return !f.err
}); });
});
}); if (res) {
return callback(null, res.scanner);
}
callback(new Error('No scanner found'));
}
);
} }
/**
* Scan the networks with the scanner detected before
* @param callback
*/
function scanNetworks(callback) { function scanNetworks(callback) {
exec(scanner.cmdLine, function (err, stdout) { exec(scanner.cmdLine, function (err, stdout) {
if (err) { if (err) {
@ -50,7 +76,7 @@ module.exports = {
*/ */
scan: function (callback) { scan: function (callback) {
if (!scanner) { if (!scanner) {
initTools((err, s) => { initTools(function (err, s) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -61,4 +87,4 @@ module.exports = {
} }
scanNetworks(callback); scanNetworks(callback);
} }
}; };

View File

@ -5,6 +5,7 @@
const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'; const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
const cmdLine = tool + ' -s'; const cmdLine = tool + ' -s';
const detector = tool + ' -getInfo';
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 macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/;
/** /**
@ -30,7 +31,7 @@ function parseOutput(str, callback) {
'ssid' : lines[i].substr(0, macStart).trim(), 'ssid' : lines[i].substr(0, macStart).trim(),
'mac' : elements[0].trim(), 'mac' : elements[0].trim(),
'channel' : parseInt(elements[2].trim(), 10), 'channel' : parseInt(elements[2].trim(), 10),
'rssi' : parseInt(elements[1].trim()) 'rssi' : parseInt(elements[1].trim(), 10)
}); });
} }
} }
@ -45,5 +46,6 @@ function parseOutput(str, callback) {
module.exports = { module.exports = {
parseOutput: parseOutput, parseOutput: parseOutput,
cmdLine : cmdLine, cmdLine : cmdLine,
detector : detector,
tool : tool tool : tool
}; };

View File

@ -4,8 +4,12 @@
*/ */
const _ = require('lodash'); const _ = require('lodash');
const tool = '/usr/bin/iwlist'; // usually located in /usr/bin/ but as it could be at another location, allow all found in PATH (but we're in trouble
// when the default location, /usr/bin/ is not in the PATH!). GitHub issue #1
const tool = 'iwlist';
const cmdLine = tool + ' scan'; const cmdLine = tool + ' scan';
const detector = tool + ' --help';
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 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:/; const cellRegex = /Cell [0-9]{2,} - Address:/;
@ -79,5 +83,6 @@ function parseOutput(str, callback) {
module.exports = { module.exports = {
parseOutput: parseOutput, parseOutput: parseOutput,
cmdLine : cmdLine, cmdLine : cmdLine,
detector : detector,
tool : tool tool : tool
}; };

View File

@ -6,6 +6,7 @@
const systemRoot = process.env.SystemRoot || 'C:\\Windows'; const systemRoot = process.env.SystemRoot || 'C:\\Windows';
const tool = systemRoot + '\\System32\\netsh.exe'; const tool = systemRoot + '\\System32\\netsh.exe';
const cmdLine = tool + ' wlan show networks mode=Bssid'; const cmdLine = tool + ' wlan show networks mode=Bssid';
const detector = tool + ' show alias';
/** /**
* Parsing netnsh output. Unfortunately netsh supplies the network information * Parsing netnsh output. Unfortunately netsh supplies the network information
@ -71,7 +72,7 @@ function parseOutput(str, callback) {
// A tricky one: the channel is the first one having just ONE number. Set only // A tricky one: the channel is the first one having just ONE number. Set only
// if the channel is not already set ("Basic Rates" can be a single number also) // if the channel is not already set ("Basic Rates" can be a single number also)
if (regexChannel.exec(lines[t].trim())) { if (regexChannel.exec(lines[t].trim())) {
network.channel = parseInt(lines[t].split(':')[1].trim()); network.channel = parseInt(lines[t].split(':')[1].trim(), 10);
} }
} }
} }
@ -90,5 +91,6 @@ function parseOutput(str, callback) {
module.exports = { module.exports = {
parseOutput: parseOutput, parseOutput: parseOutput,
cmdLine : cmdLine, cmdLine : cmdLine,
detector : detector,
tool : tool tool : tool
}; };

1640
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "node-wifi-scanner", "name": "node-wifi-scanner",
"version": "0.1.0", "version": "1.1.1",
"description": "node.js module for WiFi network detection", "description": "node.js module for WiFi network detection",
"main": "index.js", "main": "index.js",
"keywords": [ "keywords": [
@ -35,11 +35,12 @@
"test": "mocha test" "test": "mocha test"
}, },
"dependencies": { "dependencies": {
"lodash": "4.8.1" "async": "2.6.2",
"lodash": "^4.17.11"
}, },
"devDependencies": { "devDependencies": {
"grunt": "0.4.5", "grunt": "^1.0.3",
"grunt-bump": "0.7.0", "grunt-bump": "0.8.0",
"mocha": "2.2.5" "mocha": "^6.0.2"
} }
} }