Skip to content

Instantly share code, notes, and snippets.

@Maksims
Created July 25, 2013 22:03
Show Gist options
  • Save Maksims/6084210 to your computer and use it in GitHub Desktop.
Save Maksims/6084210 to your computer and use it in GitHub Desktop.
node.js function (for Windows) to get list of SSIDs of wifi networks available, their BSSID and RSSI (Signal Strength). It uses `netsh` windows command line tool to get data. (tested on win7)
var spawn = require('child_process').spawn;
wifiNetworksRSSI(function(err, data, raw) {
if (!err) {
console.log(data); // formatted data with SSID, BSSID, RSSI
// console.log(raw); // raw output from netsh
} else {
console.log(err);
}
});
function wifiNetworksRSSI(fn) {
// prepare result string of data
var res = '';
// spawn netsh with required settings
var netsh = spawn('netsh', ['wlan', 'show', 'networks', 'mode=bssid']);
// get data and append to main result
netsh.stdout.on('data', function (data) {
res += data;
});
// if error occurs
netsh.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
// when done
netsh.on('close', function (code) {
if (code == 0) { // normal exit
// prepare array for formatted data
var networks = [ ];
// split response to blocks based on double new line
var raw = res.split('\r\n\r\n');
// iterate through each block
for(var i = 0; i < raw.length; ++i) {
// prepare object for data
var network = { };
// parse SSID
var match = raw[i].match(/^SSID [0-9]+ : (.+)/);
if (match && match.length == 2) {
network.ssid = match[1];
} else {
network.ssid = '';
}
// if SSID parsed
if (network.ssid) {
// parse BSSID
var match = raw[i].match(' +BSSID [0-9]+ +: (.+)');
if (match && match.length == 2) {
network.bssid = match[1];
} else {
network.bssid = '';
}
// parse RSSI (Signal Strength)
var match = raw[i].match(' +Signal +: ([0-9]+)%');
if (match && match.length == 2) {
network.rssi = parseInt(match[1]);
} else {
network.rssi = NaN;
}
// push to list of networks
networks.push(network);
}
}
// callback with networks and raw data
fn(null, networks, res);
} else {
// if exit was not normal, then throw error
fn(new Error(code));
}
});
}
@gargoor
Copy link

gargoor commented Nov 11, 2018

would this code work on visual c ? am not abble to run even the first line, do i need to download a file or something ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment