]> code.delx.au - monosys/blob - wifi-scan
wifi-scan: linter fixes
[monosys] / wifi-scan
1 #!/usr/bin/env node
2 'use strict'
3
4 const {exec} = require('child_process');
5
6 function execAsync(command, opts) {
7 return new Promise((resolve, reject) => {
8 exec(command, opts, (error, stdout, stderr) => {
9 if (error) {
10 reject(error);
11 } else {
12 resolve({stdout, stderr});
13 }
14 });
15 });
16 }
17
18 function sleep(n) {
19 return new Promise((resolve) => setTimeout(resolve, n));
20 }
21
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]);
28 return lines[0];
29 }
30
31 async function scanInterface(iface) {
32 const {stdout} = await execAsync(`sudo iw dev ${iface} scan`);
33
34 const results = [];
35 let partial = null;
36
37 for (let line of stdout.split('\n')) {
38 if (line.startsWith('BSS ')) {
39 finishPartial();
40 partial = {};
41 partial.bssid = line.match(/[a-z0-9:]+/)[0];
42 }
43
44 line = line.trim()
45 if (line.startsWith('SSID: ')) {
46 partial.ssid = line.split(':')[1].trim();
47 }
48 if (line.startsWith('signal: ')) {
49 partial.signal = line.split(':')[1].trim();
50 }
51 if (line.startsWith('DS Parameter set: channel')) {
52 partial.channel = line.split(':')[1].trim();
53 }
54 }
55
56 function finishPartial() {
57 if (!partial) {
58 return;
59 }
60
61 partial.ssid = partial.ssid || '';
62 partial.channel = partial.channel || '';
63
64 const sortKey = [
65 parseFloat(partial.signal),
66 parseInt(partial.channel.split(' ')[1])
67 ];
68
69 results.push([sortKey, partial]);
70 }
71
72 return results
73 .sort()
74 .map(([, {bssid, ssid, signal, channel}]) => {
75 ssid = ssid.padStart(40, ' ');
76 channel = channel.padEnd(10, ' ');
77 return `${signal} ${channel} ${ssid} ${bssid}`;
78 })
79 .join('\n') + '\n';
80 }
81
82 async function main() {
83 const iface = await findInterface();
84
85 for (;;) {
86 const scanResult = await scanInterface(iface).catch((err) => err.toString());
87 process.stdout.write('\x1b[2J\x1b[0f');
88 process.stdout.write(scanResult);
89 await sleep(3000);
90 }
91 }
92
93 main().catch((err) => {
94 console.log('Unhandled error!', err);
95 process.exit(1);
96 });