]>
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];
49 if (line
.startsWith('SSID: ')) {
50 partial
.ssid
= line
.split(':')[1].trim();
52 if (line
.startsWith('signal: ')) {
53 partial
.signal
= line
.split(':')[1].trim();
55 if (line
.startsWith('DS Parameter set: channel')) {
56 partial
.channel
= line
.split(':')[1].trim();
58 if (line
.startsWith('* primary channel:')) {
59 partial
.channel
= 'channel ' + line
.split(':')[1].trim();
61 if (line
.startsWith('freq: ')) {
62 partial
.freq
= 'freq ' + line
.split(':')[1].trim();
66 function finishPartial() {
71 partial
.ssid
= partial
.ssid
|| '';
72 partial
.channel
= partial
.channel
|| partial
.freq
|| '';
75 parseFloat(partial
.signal
),
76 parseInt(partial
.channel
.split(' ')[1])
79 results
.push([sortKey
, partial
]);
84 .map(([, {bssid
, ssid
, signal
, channel
}]) => {
85 ssid
= ssid
.padStart(40, ' ').substr(0, 40);
86 channel
= channel
.padEnd(12, ' ');
87 return `${signal} ${channel} ${ssid} ${bssid}`;
92 async
function main() {
93 const iface
= process
.argv
[2] || await
findInterface();
96 const scanResult
= await fs
.readFile('/dev/stdin', 'utf-8');
97 const prettyScanResult
= formatScanResult(scanResult
);
98 process
.stdout
.write(prettyScanResult
);
101 const scanResult
= await
scanInterface(iface
).catch((err
) => err
.toString());
102 const prettyScanResult
= formatScanResult(scanResult
);
103 process
.stdout
.write('\x1b[2J\x1b[0f');
104 process
.stdout
.write(prettyScanResult
);
110 main().catch((err
) => {
111 console
.log('Unhandled error!', err
);