Compare commits

...

62 Commits

Author SHA1 Message Date
e5f9390ba1 Fix issue when using useSudo on windows/osx 2024-03-28 17:28:19 +01:00
c5cbde1a3c Fix issue when using sudo that spawn sometimes will result in error 2024-03-28 02:53:16 +01:00
e69c735d64 Set output to empty array instead of null if none 2024-03-28 02:31:48 +01:00
40ab464995 Fix sudo 2024-03-28 02:27:45 +01:00
0672168541 Fix prompt with sudo 2024-03-28 01:57:29 +01:00
4b379b106a Fix stdin bug with sudo pw 2024-03-28 00:25:39 +01:00
1f72f00d2d Merge branch 'master' of https://git.gaminggeneration.de/tobiash/node-wifi-scanner 2024-03-27 23:32:28 +01:00
a5ad7ea8a5 Change jsdoc; Fix lang 2024-03-27 23:32:04 +01:00
c16a65dbcd README.md aktualisiert 2024-03-27 22:11:38 +00:00
debd06f258 Fix null error; Fix promise 2024-03-27 22:16:56 +01:00
4e8c3efe6f test/airport.js gelöscht 2024-03-27 21:01:34 +00:00
330e851545 README.md aktualisiert 2024-03-27 21:00:57 +00:00
897314ee52 types/index.d.ts aktualisiert 2024-03-27 20:29:38 +00:00
4b751522b9 Add sudo 2024-03-27 20:28:43 +00:00
a4820acc34 index.js aktualisiert 2024-03-27 20:14:30 +00:00
b032feac3e index.d.ts aktualisiert 2024-03-27 20:05:05 +00:00
4a53c2f42a package.json aktualisiert 2024-03-27 19:58:49 +00:00
7314228c91 Dateien nach "/" hochladen 2024-03-27 19:55:54 +00:00
95b62bfbb0 README.md aktualisiert 2024-03-27 19:51:56 +00:00
91c43c5aac README.md aktualisiert 2024-03-27 19:50:58 +00:00
Christian Kuster
0f1a6d6661 New version added v1.1.3 2023-01-24 21:39:02 +01:00
Christian Kuster
9d42fb9b5e Improving mac OS part 2023-01-24 21:35:10 +01:00
Christian Kuster
0fe7bd8eed Merge remote-tracking branch 'origin/master'
# Conflicts:
#	package.json
2023-01-24 21:26:35 +01:00
Christian Kuster
a8416e1a27
Merge pull request #19 from soitgoes/auditfix
Auditfix
2023-01-24 21:25:59 +01:00
Christian Kuster
d7715d1cdf Merge remote-tracking branch 'origin/master' 2023-01-24 21:24:40 +01:00
Christian Kuster
36e1f8ffaf
Merge pull request #15 from soitgoes/master
Accomodate for apples elimination of BSSID from airport results
2023-01-24 21:24:15 +01:00
Christian Kuster
cf885a3bdd Dependency updates only 2023-01-24 21:23:10 +01:00
Martin Murphy
ba0bfc7d42 upgrade npm modules causing audit flags 2022-12-21 10:25:35 -06:00
Martin Murphy
d0f22a8ed2 Remove unused var 2022-01-08 11:28:21 -06:00
Martin Murphy
a021a5d469 Accomodate for apples elimination of BSSID from airport results 2022-01-08 11:24:23 -06:00
Christian Kuster
9fe4300976 npm changed package.json 2019-10-27 15:08:32 +01:00
Christian Kuster
8eeb0a9827 Updated version in package.json 2019-10-27 14:35:22 +01:00
Christian Kuster
d140e224c9 ReadMe updated 2019-10-27 14:21:45 +01:00
Christian Kuster
ef25a7fb7c info that you need to be superuser in linux in order to see all wifi networks 2019-09-21 15:49:06 +02:00
Christian Kuster
32b516e302 Dependency updates, some code cleaned up 2019-09-21 11:56:22 +02:00
Christian Kuster
d1eac84058
Merge pull request #8 from aydogankaragoz/master
Fixed channel NaN issue for 5Ghz networks.
2019-09-21 11:21:45 +02:00
Aydoğan Karagöz
50090aecda
Update iwlist.js 2019-09-11 16:59:05 +03:00
Christian Kuster
bc61dc2f02 New version added v1.1.1 2019-04-09 19:49:35 +02:00
Christian Kuster
5030d9f693 Package updates 2019-04-09 19:48:21 +02:00
Christian Kuster
e874b44200
Merge pull request #2 from sylvaingirardbe/remove-npm-vulnerabilities
Update packages
2019-04-08 15:04:44 +02:00
sylvain@ecm-technologies.eu
f1c0c014da Update to current version of node 2019-03-06 16:08:22 +01:00
sylvain@ecm-technologies.eu
29e715897e Update packages 2019-03-06 16:00:12 +01:00
Christian Kuster
d302473da7 Merge branch 'develop' 2016-12-09 19:13:17 +01:00
Christian Kuster
5305124084 New version added v1.1.0 2016-12-09 19:12:16 +01:00
Christian Kuster
cfacdd6438 node 4 compliant again 2016-12-09 17:53:43 +01:00
Christian Kuster
8a8005e2ef Windows fixes (work still in progress) 2016-12-09 17:04:27 +01:00
Christian Kuster
6ce58993a2 Refactoring tool detection, work in progress 2016-12-09 16:55:17 +01:00
Christian Kuster
834dc4210e Test for GitHub issue #1: removed absolute path for iwlist 2016-12-09 09:25:20 +01:00
Christian Kuster
b33de0ce94 Lint issues only 2016-06-08 19:41:44 +02:00
Christian Kuster
77da84a261 Merge branch 'develop'
# Conflicts:
#	README.md
2016-05-03 14:55:40 +02:00
Christian Kuster
26fff7e5d8 New version added v1.0.0 2016-05-03 14:53:52 +02:00
Christian Kuster
9669064662 readme.md fixed: removed nmcli 2016-04-12 17:25:00 +02:00
Christian Kuster
6f44b5b039 New version added v0.1.0 2016-04-12 17:22:28 +02:00
Christian Kuster
2caacc853a mmcli removed: using iwlist on linux systems, output was not really comparable to other tools 2016-04-12 17:22:11 +02:00
Christian Kuster
de5cde2ca9 New version added v0.0.5 2016-04-07 14:39:08 +02:00
Christian Kuster
7cf077d527 Windows Bugfix 2016-04-07 14:38:42 +02:00
Christian Kuster
253b375923 Another tags added 2016-04-07 13:25:28 +02:00
Christian Kuster
f22300b438 Build status added 2016-04-07 13:19:42 +02:00
Christian Kuster
a15dd29c53 travis added, package.json fixes 2016-04-07 13:14:27 +02:00
Christian Kuster
d01b3cc124 New version added v0.0.4 2016-04-07 06:20:37 +02:00
Christian Kuster
31d80834a7 npm and node version where to strict 2016-04-07 06:20:18 +02:00
Christian Kuster
1aff4ba049 Installation info added 2016-04-06 22:02:32 +02:00
18 changed files with 3547 additions and 303 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea
node_modules
/yarn.lock

8
.travis.yml Normal file
View File

@ -0,0 +1,8 @@
language: node_js
branches:
only:
- master
- develop
node_js:
- "10"
- "11"

View File

@ -1,4 +1,47 @@
#node-wifi-scanner
# Fork of node-wifi-scanner
This fork is used to add the ability to scan as sudo under linux.
Why the fork?
- Add ability to execute command with prefix 'sudo' to get iwlist working on linux!
- Uses promise instead of callback in module exports to make it easier and prevent callback hells.
- Add `encrypted:boolean` to the output, to determine if the network is encrypted (Currently only works under iwlist/linux, otherwise just returns false)
To use sudo, just pass `true` to the scan method and it will use sudo.
For ex.
```js
const wifiScan = require("node-wifi-scanner");
let result = await wifiScan.scan(true);
```
Beforehand you should add the iwlist scan command to the sudoers file.
`%mygroup ALL = (root) NOPASSWD: iwlist wlanX scan` [src](https://stackoverflow.com/questions/53710368/how-do-i-add-typescript-types-to-a-javascript-module-without-switching-to-typesc)
### Installation
`yarn add git+https://github.com/Tobstr02/node-wifi-scanner.git`
or `npm install git+https://github.com/Tobstr02/node-wifi-scanner.git`
### Type definitions
I also tried to add type definitions (see /types/index.d.ts), but as far as i know i did it wrong haha.
Since this is my first ever module (-fork) i don't really know how to do it. - Let me know in the issues tab or create a pull request :)
But i added jsdoc to the functions to add the ability to make the coding process a bit cleaner.
<br>
<i>Fork from: [Node-Wifi-Scanner Github](https://github.com/ancasicolica/node-wifi-scanner/)</i>
<br><br>
----
# Node-Wifi-Scanner
[![Build Status](https://travis-ci.org/ancasicolica/node-wifi-scanner.svg?branch=master)](https://travis-ci.org/ancasicolica/node-wifi-scanner)
[![npm](https://img.shields.io/npm/v/node-wifi-scanner.svg)]()
[![npm](https://img.shields.io/npm/dt/node-wifi-scanner.svg)](https://www.npmjs.com/package/node-wifi-scanner)
This module for node.js scans available wifi networks. The main purpose was to enhance my node.js based
[ZigBee Site Survey Tool](http://ancasicolica.github.io/ZigBeeSiteSurvey/) with WiFi coexistence charts. This tool
@ -9,34 +52,27 @@ 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
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
It was tested with the following operating systems:
* Mac OS-X
* Windows 10
* Ubuntu 14.04
* Raspbian "Jessie"
* Mac OS-X [Not tested by fork author]
* Windows 10 [Not tested by fork author]
* Ubuntu 14.04 [Tested by fork author]
* Raspbian "Jessie" [Tested by fork author]
## Installation
yarn add git+https://git.gaminggeneration.de/tobiash/node-wifi-scanner
## Usage
const scanner = require('node-wifi-scanner');
scanner.scan((err, networks) => {
if (err) {
console.error(err);
return;
}
console.log(networks);
});
The tool returns an array with objects, each object representing a network with the following properties:
* channel: WiFi channel
* ssid: SSID of the network (if available)
* mac: MAC Address of the network access point
* mac: MAC Address of the network access point (if available, otherwise empty string)
* rssi: signal strength
* encrypted: boolean - Is network encrypted?
In contrary to other wifi scanners no information about security is returned. This is due to the very different implementation
of the command line tools which do not allow a flawless detection.
@ -47,18 +83,34 @@ The module uses command line tools for gathering the network information:
* airport on Mac OS-X: `airport -s`
* netsh on Windows: `netsh wlan show networks mode=Bssid`
* iwlist (1st choice) on Linux: `iwlist scan`
* nmcli (fallback only) on Linux: `nmcli -m tabular -f SSID,BSSID,SIGNAL,FREQ device wifi`
* iwlist on Linux: `iwlist scan`
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
the output of the tool.
## Limits of the tool
There is no such thing as perfect software and this is all the more true when the tools used require different
access rights depending on
the operating system. Please note the following restrictions
before using this tool in a productive system.
**Linux**: iwlist does only return all found networks if run as sudo! Otherwise you'll
get only the network you're connected to. <i>That's where my fork comes in!!</i>
**Windows**: there are some network cards which do not
return the MAC address and other parameters of the found networks. In this case
the "found" networks are ignored as there is no valuable data. If you have this effect
on your system, please provide as many information about your system (PC manufacturer, network
card, OS,...) as available. Thanks
## Licence
The MIT License (MIT)
Copyright (c) 2016 Christian Kuster
Copyright (c) 2016-2024 Christian Kuster, Tobias Hopp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -77,3 +129,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -5,7 +5,7 @@
* Created by kc on 04.04.16.
*/
var scanner = require('../index');
const scanner = require('../index');
scanner.scan((err, output) => {
if (err) {

150
index.js
View File

@ -1,73 +1,143 @@
/**
* node-wifi-scanner
* Created by kc on 04.04.16.
* @typedef {Object} WifiNetwork
* @property {string} ssid - SSID of the wifi
* @property {string} mac - MAC-Address of the wifi
* @property {number} channel - Channel of the wifi
* @property {number} rssi - Signalstrength of Wifi-Network (RSSI)
* @property {boolean} encrypted - Encrypted
*/
const fs = require('fs');
const exec = require('child_process').exec;
/**
* node-wifi-scanner
* Created by kc on 04.04.16. - Forked and updated by Tobias Hopp 27.03.2024
*/
const {exec, spawn} = require('child_process');
const async = require('async');
const _ = require('lodash');
// The tools
const airport = require('./lib/airport');
const iwlist = require('./lib/iwlist');
const nmcli = require('./lib/nmcli');
const netsh = require('./lib/netsh');
var scanner;
let scanner;
// Initializing the tools
function initTools(callback) {
fs.stat(airport.tool, function (err, stats) {
if (stats) {
return callback(null, airport);
}
fs.stat(iwlist.tool, function (err, stats) {
if (stats) {
return callback(null, iwlist);
}
fs.stat(nmcli.tool, function (err, stats) {
if (stats) {
return callback(null, nmcli);
}
fs.stat(netsh.tool, function (err, stats) {
if (stats) {
return callback(null, netsh);
// When a command is not found, an error is issued and async would finish. Therefore we pack
// the error into the result and check it later on.
async.parallel([
function (cb) {
exec(airport.detector, function (err) {
cb(null, {err: err, scanner: airport}
)
}
callback(new Error('No scanner found'));
);
},
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}
)
}
);
}
],
function (err, results) {
let res = _.find(results,
function (f) {
return !f.err
});
});
});
});
if (res) {
return callback(null, res.scanner);
}
callback(new Error('No scanner found'));
}
);
}
function scanNetworks(callback) {
exec(scanner.cmdLine, function (err, stdout) {
/**
* Scan the networks with the scanner detected before
* @param {Function|any} callback
* @param {boolean} useSudo - Use sudo for access?
*/
function scanNetworks(callback, useSudo) {
const cmd = scanner.cmdLine.split(" ");
let args = cmd.slice(1);
if(useSudo)
args.unshift(cmd[0]);
const child = spawn(useSudo ? "sudo" : cmd[0], args, { stdio: ['ignore', 'pipe', 'pipe'], detached: true });
child.on("error", (err) => callback(err, null) );
let stdoutData = '';
child.stdout.on('data', (data) => {
stdoutData += data;
});
child.on('close', (code) => {
if (code === 0) {
// Prozess erfolgreich beendet, Ausgabe verarbeiten
scanner.parseOutput(stdoutData, callback);
} else {
// Prozess mit Fehler beendet
callback(new Error(`Exited with code ${code}`), null);
}
});
/*exec((useSudo ? "sudo ":"") + scanner.cmdLine, { shell: true }, function (err, stdout) {
if (err) {
callback(err, null);
return;
}
scanner.parseOutput(stdout, callback);
});
});*/
}
module.exports = {
/**
* Scan for wifis
* @param callback
*/
scan: function (callback) {
if (!scanner) {
initTools((err, s) => {
* Scan for wifi networks
* @param {boolean} useSudo? - Defaults to false | Should sudo be used to get the output?
* @return {Promise<WifiNetwork[]|null>} WiFinetwork Array or null
* @rejects Returns error on reject
*/
scan: function (useSudo = false) {
return new Promise( (resolve, reject) => {
if (!scanner) {
initTools(function (err, s) {
if (err) {
return callback(err);
return reject(err);
}
scanner = s;
scanNetworks(callback);
scanNetworks((err, result) => {
if(err)
return reject(err);
if(!result)
result = [];
resolve(result);
}, useSudo && scanner == iwlist);
});
return;
}
scanNetworks(callback);
scanNetworks((err, result) => {
if(err)
return reject(err);
if(!result)
result = [];
resolve(result);
}, useSudo && scanner == iwlist);
} );
}
};

View File

@ -3,47 +3,46 @@
* Created by kc on 04.04.16.
*/
const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
const cmdLine = tool + ' -s';
const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
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}/;
/**
* Parsing the output of airport (Mac OS X)
* @param str output of the tool
* @param callback
*/
function parseOutput(str, callback) {
var err = null;
let err = null;
let wifis = [];
try {
var lines = str.split('\n');
var wifis = [];
let lines = str.split('\n');
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(/[ ]+/);
for (let i = 1, l = lines.length; i < l; i++) {
if (lines[i] === '') continue;
let elements = lines[i].substring(51).split(/[ ]+/);
wifis.push({
'ssid' : lines[i].substr(0, macStart).trim(),
'mac' : elements[0].trim(),
'channel' : parseInt(elements[2].trim(), 10),
'rssi' : parseInt(elements[1].trim())
'ssid' : lines[i].substring(0, 32).trim(),
'mac' : '', // There is no more mac on a mac
'channel': parseInt(elements[1].trim(), 10),
'rssi' : parseInt(elements[0].trim(), 10),
'encrypted': false,
});
}
}
catch (ex) {
err = ex;
}
callback(err, wifis);
finally {
callback(err, wifis);
}
}
module.exports = {
parseOutput: parseOutput,
cmdLine : cmdLine,
detector : detector,
tool : tool
};

View File

@ -3,27 +3,34 @@
* Created by kc on 04.04.16.
*/
const _ = require('lodash');
const tool = '/usr/bin/iwlist';
const cmdLine = tool + ' scan';
const _ = require('lodash');
// 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 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 cellRegex = /Cell [0-9]{2,} - Address:/;
const encryptedRegex = /Encryption key:on/g;
/**
* 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 = [];
let err = null;
let wifis = [];
try {
var blocks = str.split(cellRegex);
let blocks = str.split(cellRegex);
blocks.forEach(block => {
var network = {};
var lines = block.split('\n');
let network = {encrypted: encryptedRegex.test(block)};
let lines = block.split('\n');
if (macRegex.exec(lines[0])) {
// First line is the mac address (always! (?))
network.mac = lines[0].trim();
@ -39,8 +46,8 @@ function parseOutput(str, callback) {
}
// Channel, an ugly thing to get it
else if (_.startsWith(line.trim(), 'Frequency:')) {
network.channel = parseInt(_.trim(line, ' )').split(/Channel/)[1], 10);
else if (_.startsWith(line.trim(), 'Channel:')) {
network.channel = parseInt(_.trim(line, ' )').split(/:/)[1]);
}
// Another ugly thing, the signal which can have different formats, even worse als
@ -49,15 +56,14 @@ function parseOutput(str, callback) {
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 {
} else {
// This is a "Signal level=60/100" line
var elements = line.split('=');
let 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));
let parts = e.split('/');
let level = Math.floor(100 * parseInt(parts[0], 10) / parseInt(parts[1], 10));
network.rssi = level / 2 - 100;
}
})
@ -79,5 +85,6 @@ function parseOutput(str, callback) {
module.exports = {
parseOutput: parseOutput,
cmdLine : cmdLine,
detector : detector,
tool : tool
};

View File

@ -6,6 +6,7 @@
const systemRoot = process.env.SystemRoot || 'C:\\Windows';
const tool = systemRoot + '\\System32\\netsh.exe';
const cmdLine = tool + ' wlan show networks mode=Bssid';
const detector = tool + ' show alias';
/**
* Parsing netnsh output. Unfortunately netsh supplies the network information
@ -14,13 +15,17 @@ const cmdLine = tool + ' wlan show networks mode=Bssid';
* an approach of analyzing the structure of the output
*/
function parseOutput(str, callback) {
var blocks = str.split('\n\n');
var wifis = [];
var err = null;
let blocks = str.split('\n\n');
let wifis = [];
let err = null;
try {
if (blocks.length < 2) {
// 2nd try, with \r\n
blocks = str.split('\r\n\r\n')
}
if (!blocks || blocks.length === 1) {
// No WiFis found
return [];
return callback(null, []);
}
// Each block has the same structure, while some parts might be available and others
@ -35,18 +40,18 @@ function parseOutput(str, callback) {
// Channel : 6
// Basic rates (MBit/s) : 1 2 5.5 11
// Other rates (MBit/s) : 6 9 12 18 24 36 48 54
for (var i = 1, l = blocks.length; i < l; i++) {
var network = {};
var lines = blocks[i].split('\n');
var regexChannel = /[a-zA-Z0-9()\s]+:[\s]*[0-9]+$/g;
for (let i = 1, l = blocks.length; i < l; i++) {
let network = {encrypted: false};
let lines = blocks[i].split('\n');
let regexChannel = /[a-zA-Z0-9()\s]+:[\s]*[0-9]+$/g;
if (!lines || lines.length < 2) {
continue;
}
// First line is always the SSID (which can be empty)
var ssid = lines[0].substring(lines[0].indexOf(':') + 1).trim();
let ssid = lines[0].substring(lines[0].indexOf(':') + 1).trim();
for (var t = 1, n = lines.length; t < n; t++) {
for (let t = 1, n = lines.length; t < n; t++) {
if (lines[t].split(':').length === 7) {
// This is the mac address, use this one as trigger for a new network
if (network.mac) {
@ -56,18 +61,16 @@ function parseOutput(str, callback) {
ssid: ssid,
mac : lines[t].substring(lines[t].indexOf(':') + 1).trim()
};
}
else if (lines[t].indexOf('%') > 0) {
} else if (lines[t].indexOf('%') > 0) {
// Network signal strength, identified by '%'
var level = parseInt(lines[t].split(':')[1].split('%')[0].trim(), 10);
let level = parseInt(lines[t].split(':')[1].split('%')[0].trim(), 10);
network.rssi = (level / 2) - 100;
}
else if (!network.channel) {
} else if (!network.channel) {
// 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 (regexChannel.exec(lines[t].trim())) {
network.channel = parseInt(lines[t].split(':')[1].trim());
network.channel = parseInt(lines[t].split(':')[1].trim(), 10);
}
}
}
@ -86,5 +89,6 @@ function parseOutput(str, callback) {
module.exports = {
parseOutput: parseOutput,
cmdLine : cmdLine,
detector : detector,
tool : tool
};

View File

@ -1,50 +0,0 @@
/**
* Scanning WiFis on Mac OS X using nmcli
* Created by kc on 04.04.16.
*/
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())
});
}
}
catch (ex) {
err = ex;
}
callback(err, wifis);
}
module.exports = {
parseOutput: parseOutput,
cmdLine : cmdLine,
tool : tool
};

3219
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,36 +1,45 @@
{
"name": "node-wifi-scanner",
"version": "0.0.3",
"version": "1.1.3",
"description": "node.js module for WiFi network detection",
"main": "index.js",
"keywords": [
"WiFi",
"Node.js",
"scanner"
"scanner",
"airport",
"netsh",
"iwlist",
"nmcli"
],
"author": {
"name": "Christian Kuster, CH-8342 Wernetshausen",
"email": "info@kusti.ch",
"url": "http://www.kusti.ch/"
"name": "Christian Kuster, Tobias Hopp",
"email": "tobi@gaminggeneration.de"
},
"homepage": "https://git.gaminggeneration.de/TobiasH/node-wifi-scanner/",
"bugs": {
"url": "https://git.gaminggeneration.de/TobiasH/node-wifi-scanner/issues/",
"email": "info@ancasicolica.ch"
},
"homepage": "http://www.ferropoly.ch/",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/ancasicolica/node-wifi-scanner.git"
"url": "https://git.gaminggeneration.de/TobiasH/node-wifi-scanner.git"
},
"engines": {
"node": ">= 4.4.0",
"npm": ">= 2.14.0"
"node": ">= 4.0.0",
"npm": ">= 2.0.0"
},
"scripts": {
"test": "mocha test"
},
"dependencies": {
"lodash": "4.8.1"
"async": "3.2.4",
"lodash": "4.17.21"
},
"devDependencies": {
"grunt": "0.4.5",
"grunt-bump": "0.7.0"
"grunt": "1.5.3",
"grunt-bump": "0.8.0",
"mocha": "10.2.0"
}
}

View File

@ -1,67 +0,0 @@
/**
* Airport unit test
* Created by kc on 04.04.16.
*/
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const airport = require('../lib/airport');
describe('airport', () => {
it('parses the output of file 1', function(done) {
airport.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','airport','airport01.txt'), { encoding: 'utf8' }), (err, info) => {
assert.ok(info);
assert.equal(info.length, 36);
var ap = info[0];
assert.equal(ap.mac, '00:35:1a:90:56:03');
assert.equal(ap.ssid, 'OurTest');
assert.equal(ap.rssi, -70);
assert.strictEqual(ap.channel, 112);
ap = info[19];
assert.equal(ap.mac, '00:35:1a:5b:45:b6');
assert.equal(ap.ssid, 'PDANet1');
assert.equal(ap.rssi, -78);
assert.strictEqual(ap.channel, 11);
ap = info[25];
assert.equal(ap.mac, '10:bd:18:ab:4d:8f');
assert.equal(ap.ssid, 'TEST Training');
assert.equal(ap.rssi, -71);
assert.strictEqual(ap.channel, 6);
ap = info[35];
assert.equal(ap.mac, '00:35:1a:90:56:00');
assert.equal(ap.ssid, 'TEST-Wifi');
assert.equal(ap.rssi, -67);
assert.strictEqual(ap.channel, 1);
done(err);
});
});
it('parses the output of file 2', function(done) {
airport.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','airport','airport02.txt'), { encoding: 'utf8' }), (err, info) => {
assert.ok(info);
assert.equal(info.length, 4);
var ap = info[0];
assert.equal(ap.mac, '7c:b7:33:ae:3b:06');
assert.equal(ap.ssid, 'Raupo');
assert.equal(ap.rssi, -80);
assert.strictEqual(ap.channel, 64);
ap = info[3];
assert.equal(ap.mac, '7c:b7:33:ae:3b:04');
assert.equal(ap.ssid, 'Visitor Raupo');
assert.equal(ap.rssi, -66);
assert.strictEqual(ap.channel, 9);
done(err);
});
});
});

View File

@ -1,5 +1,5 @@
SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
OurTest 00:35:1a:90:56:03 -70 112 Y CH WPA2(PSK/AES/AES)
OurTest -70 112 Y CH WPA2(PSK/AES/AES)
OurDev 00:35:1a:90:56:04 -70 112 Y CH WPA2(PSK/AES/AES)
PDANet1 00:35:1a:90:56:09 -70 112 Y CH WPA2(PSK/AES/AES)
TEST-Wifi 00:35:1a:90:56:0f -70 112 Y CH WPA2(PSK/AES/AES)

View File

@ -1,20 +0,0 @@
SSID BSSID SIGNAL FREQUENZ
'PDANet1' 00:35:1A:90:56:06 100 2412 MHz
'ExoNet1' 00:35:1A:90:56:05 100 2412 MHz
'TEST-Wifi' 00:35:1A:90:56:00 96 2412 MHz
'OurTest' 00:35:1A:90:56:0C 96 2412 MHz
'OurDev' 00:35:1A:90:56:0B 76 2412 MHz
'PDANet1' 00:35:1A:6F:0F:46 48 2437 MHz
'TEST-Wifi' 00:35:1A:6F:0F:40 58 2437 MHz
'ExoNet1' 00:35:1A:6F:0F:45 58 2437 MHz
'OurDev' 00:35:1A:6F:0F:4B 58 2437 MHz
'OurTest' 00:35:1A:6F:0F:4C 60 2437 MHz
'TEST-Wifi' 00:F2:8B:8F:58:70 58 2462 MHz
'ExoNet1' 00:F2:8B:8F:58:75 72 2462 MHz
'PDANet1' 00:F2:8B:8F:58:76 58 2462 MHz
'PDANet1' 00:35:1A:5B:46:76 56 2412 MHz
'OurDev' 00:35:1A:5B:46:7B 48 2412 MHz
'OurDev' 00:F2:8B:8F:58:7B 58 2462 MHz
'OurTest' 00:F2:8B:8F:58:7C 72 2462 MHz
'TEST-Wifi' 00:35:1A:5B:46:70 56 2412 MHz
'OurTest' 00:35:1A:5B:46:7C 48 2412 MHz

View File

@ -22,7 +22,7 @@ describe('iwlist', () => {
assert.ok(info);
var ap = info[0];
let ap = info[0];
assert.equal(ap.mac, 'D4:D1:84:50:76:45');
assert.equal(ap.ssid, 'gsy-97796');
assert.equal(ap.rssi, -76);
@ -40,7 +40,7 @@ describe('iwlist', () => {
iwlist.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','iwlist','iwlist03_raspi.txt'), { encoding: 'utf8' }), (err, info) => {
assert.ok(info);
var ap = info[0];
let ap = info[0];
assert.equal(ap.mac, '00:35:1A:90:56:00');
assert.equal(ap.ssid, 'LORA-Wifi');
assert.equal(ap.rssi, -71);

View File

@ -16,7 +16,7 @@ describe('netsh', function () {
assert.ok(info);
assert.equal(info.length, 86);
var ap = info[0];
let ap = info[0];
assert.equal(ap.mac, '00:f2:8b:8c:a6:88');
assert.equal(ap.ssid, '');
assert.equal(ap.rssi, -88.5);
@ -55,7 +55,7 @@ describe('netsh', function () {
assert.ok(info);
assert.equal(info.length, 86);
var ap = info[0];
let ap = info[0];
assert.equal(ap.mac, '00:f2:8b:8c:a6:88');
assert.equal(ap.ssid, '');
assert.equal(ap.rssi, -88.5);
@ -93,8 +93,8 @@ describe('netsh', function () {
netsh.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures', 'netsh', 'netsh_sp.txt'), {encoding: 'utf8'}), (err, info) => {
assert.ok(info);
assert.equal(info.length, 8);
console.log(info);
var ap = info[0];
let ap = info[0];
assert.equal(ap.mac, '98:fc:11:b6:88:9e');
assert.equal(ap.ssid, 'CARAMANZANAS_BAJA');
assert.equal(ap.rssi, -86);

View File

@ -1,30 +0,0 @@
/**
* 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, 19);
var ap = info[0];
assert.equal(ap.mac, '00:35:1A:90:56:06');
assert.equal(ap.ssid, 'PDANet1');
//assert.equal(ap.rssi, -70);
assert.strictEqual(ap.channel, 112);
done(err);
});
});
});

41
types/index.d.ts vendored Normal file
View File

@ -0,0 +1,41 @@
declare module 'node-wifi-scanner' {
type Callback<T> = (error: Error | null, result: T | null) => void;
interface WifiNetwork {
ssid: string;
mac: string;
channel: number;
rssi: number;
}
interface Scanner {
cmdLine: string;
parseOutput(output: string, callback: Callback<WifiNetwork[]>): void;
}
interface Tool {
detector: string;
}
const airport: Tool;
const iwlist: Tool;
const netsh: Tool;
/**
* Initialize the scanner tools
* @param callback Callback function
*/
function initTools(callback: Callback<Scanner>): void;
/**
* Scan the networks
* @param callback Callback function
*/
function scanNetworks(callback: Callback<WifiNetwork[]>): void;
/**
* Scan for WiFi networks
* @param callback Callback function
*/
function scan(callback: Callback<WifiNetwork[]>): void;
}