]>
code.delx.au - monosys/blob - wifi-scan
4 const {exec
} = require('child_process');
6 function execAsync(command
, opts
) {
7 return new Promise((resolve
, reject
) => {
8 exec(command
, opts
, (error
, stdout
, stderr
) => {
12 resolve({stdout
, stderr
});
19 return new Promise((resolve
) => setTimeout(resolve
, n
));
22 async
function findInterface() {
23 const {stdout
} = await
execAsync('iw dev');
24 const lines
= stdout
.split('\n')
25 .map((line
) => line
.trim())
26 .filter((line
) => line
.startsWith('Interface '))
27 .map((line
) => line
.split(' ')[1]);
31 async
function scanInterface(iface
) {
32 const {stdout
} = await
execAsync(`sudo iw dev ${iface} scan`);
37 for (let line
of stdout
.split('\n')) {
38 if (line
.startsWith('BSS ')) {
41 partial
.bssid
= line
.match(/[a-z0-9:]+/)[0];
45 if (line
.startsWith('SSID: ')) {
46 partial
.ssid
= line
.split(':')[1].trim();
48 if (line
.startsWith('signal: ')) {
49 partial
.signal
= line
.split(':')[1].trim();
51 if (line
.startsWith('DS Parameter set: channel')) {
52 partial
.channel
= line
.split(':')[1].trim();
54 if (line
.startsWith('freq: ')) {
55 partial
.freq
= 'freq ' + line
.split(':')[1].trim();
59 function finishPartial() {
64 partial
.ssid
= partial
.ssid
|| '';
65 partial
.channel
= partial
.channel
|| partial
.freq
|| '';
68 parseFloat(partial
.signal
),
69 parseInt(partial
.channel
.split(' ')[1])
72 results
.push([sortKey
, partial
]);
77 .map(([, {bssid
, ssid
, signal
, channel
}]) => {
78 ssid
= ssid
.padStart(40, ' ');
79 channel
= channel
.padEnd(10, ' ');
80 return `${signal} ${channel} ${ssid} ${bssid}`;
85 async
function main() {
86 const iface
= await
findInterface();
89 const scanResult
= await
scanInterface(iface
).catch((err
) => err
.toString());
90 process
.stdout
.write('\x1b[2J\x1b[0f');
91 process
.stdout
.write(scanResult
);
96 main().catch((err
) => {
97 console
.log('Unhandled error!', err
);