Compare commits
8 Commits
c16a65dbcd
...
master
Author | SHA1 | Date | |
---|---|---|---|
e5f9390ba1 | |||
c5cbde1a3c | |||
e69c735d64 | |||
40ab464995 | |||
0672168541 | |||
4b379b106a | |||
1f72f00d2d | |||
a5ad7ea8a5 |
52
index.js
52
index.js
@ -1,18 +1,20 @@
|
||||
/**
|
||||
* @typedef {Object} WifiNetwork
|
||||
* @property {string} ssid - SSID des WLAN-Netzwerks
|
||||
* @property {string} mac - MAC-Adresse des WLAN-Netzwerks
|
||||
* @property {number} channel - Kanal des WLAN-Netzwerks
|
||||
* @property {number} rssi - Signalstärke des WLAN-Netzwerks (RSSI)
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* node-wifi-scanner
|
||||
* Created by kc on 04.04.16.
|
||||
* Created by kc on 04.04.16. - Forked and updated by Tobias Hopp 27.03.2024
|
||||
*/
|
||||
|
||||
const exec = require('child_process').exec;
|
||||
const {exec, spawn} = require('child_process');
|
||||
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
// The tools
|
||||
@ -70,18 +72,42 @@ function initTools(callback) {
|
||||
* @param {boolean} useSudo - Use sudo for access?
|
||||
*/
|
||||
function scanNetworks(callback, useSudo) {
|
||||
exec((useSudo ? "sudo ":"") + scanner.cmdLine, function (err, stdout) {
|
||||
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 = {
|
||||
/**
|
||||
* Funktion zum Scannen von WLAN-Netzwerken
|
||||
* 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
|
||||
@ -97,8 +123,10 @@ module.exports = {
|
||||
scanNetworks((err, result) => {
|
||||
if(err)
|
||||
return reject(err);
|
||||
if(!result)
|
||||
result = [];
|
||||
resolve(result);
|
||||
}, useSudo);
|
||||
}, useSudo && scanner == iwlist);
|
||||
|
||||
});
|
||||
return;
|
||||
@ -106,8 +134,10 @@ module.exports = {
|
||||
scanNetworks((err, result) => {
|
||||
if(err)
|
||||
return reject(err);
|
||||
if(!result)
|
||||
result = [];
|
||||
resolve(result);
|
||||
}, useSudo);
|
||||
}, useSudo && scanner == iwlist);
|
||||
} );
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user