]>
code.delx.au - monosys/blob - bin/wifi-scan
4 const {exec
} = require('child_process');
5 const fs
= require('fs').promises
;
7 function execAsync(command
, opts
) {
8 return new Promise((resolve
, reject
) => {
9 exec(command
, opts
, (error
, stdout
, stderr
) => {
13 resolve({stdout
, stderr
});
20 return new Promise((resolve
) => setTimeout(resolve
, n
));
23 async
function findInterface() {
24 const {stdout
} = await
execAsync('iw dev');
25 const lines
= stdout
.split('\n')
26 .map((line
) => line
.trim())
27 .filter((line
) => line
.startsWith('Interface '))
28 .map((line
) => line
.split(' ')[1]);
32 async
function scanInterface(iface
) {
33 const {stdout
} = await
execAsync(`sudo iw dev ${iface} scan`);
37 function formatScanResult(scanResult
) {
41 for (let line
of scanResult
.split('\n')) {
42 if (line
.startsWith('BSS ')) {
45 partial
.bssid
= line
.match(/[a-z0-9:]+/)[0];
46 partial
.associated
= line
.indexOf('associated') >= 0 ? '**' : '';
50 if (line
.startsWith('SSID: ')) {
51 partial
.ssid
= line
.split(':')[1].trim();
53 if (line
.startsWith('signal: ')) {
54 partial
.signal
= line
.split(':')[1].trim();
56 if (line
.startsWith('DS Parameter set: channel')) {
57 partial
.channel
= line
.split(':')[1].trim();
59 if (line
.startsWith('* primary channel:')) {
60 partial
.channel
= 'channel ' + line
.split(':')[1].trim();
62 if (line
.startsWith('freq: ')) {
63 partial
.freq
= 'freq ' + line
.split(':')[1].trim();
69 function finishPartial() {
74 partial
.ssid
= partial
.ssid
|| '';
75 partial
.channel
= partial
.channel
|| partial
.freq
|| '';
78 parseFloat(partial
.signal
),
79 parseInt(partial
.channel
.split(' ')[1])
82 results
.push([sortKey
, partial
]);
87 .map(([, {bssid
, ssid
, signal
, channel
, associated
}]) => {
88 ssid
= ssid
.padStart(40, ' ').substr(0, 40);
89 channel
= channel
.padEnd(12, ' ');
90 return `${signal} ${channel} ${ssid} ${bssid} ${associated}`;
95 async
function main() {
96 const iface
= process
.argv
[2] || await
findInterface();
99 const scanResult
= await fs
.readFile('/dev/stdin', 'utf-8');
100 const prettyScanResult
= formatScanResult(scanResult
);
101 process
.stdout
.write(prettyScanResult
);
104 const scanResult
= await
scanInterface(iface
).catch((err
) => err
.toString());
105 const prettyScanResult
= formatScanResult(scanResult
);
106 process
.stdout
.write('\x1b[2J\x1b[0f');
107 process
.stdout
.write(prettyScanResult
);
113 main().catch((err
) => {
114 console
.log('Unhandled error!', err
);