]> code.delx.au - monosys/blobdiff - bin/wifi-scan
wifi-scan: show associated
[monosys] / bin / wifi-scan
index 344581ad5809ff0ed65ed91a0dd3d02dfc1b75e3..12c3240e34af533262ebaaa12bf024f0511590e8 100755 (executable)
@@ -2,6 +2,7 @@
 'use strict'
 
 const {exec} = require('child_process');
+const fs = require('fs').promises;
 
 function execAsync(command, opts) {
     return new Promise((resolve, reject) => {
@@ -30,15 +31,19 @@ async function findInterface() {
 
 async function scanInterface(iface) {
     const {stdout} = await execAsync(`sudo iw dev ${iface} scan`);
+    return stdout;
+}
 
+function formatScanResult(scanResult) {
     const results = [];
     let partial = null;
 
-    for (let line of stdout.split('\n')) {
+    for (let line of scanResult.split('\n')) {
         if (line.startsWith('BSS ')) {
             finishPartial();
             partial = {};
             partial.bssid = line.match(/[a-z0-9:]+/)[0];
+            partial.associated = line.indexOf('associated') >= 0 ? '**' : '';
         }
 
         line = line.trim()
@@ -51,11 +56,16 @@ async function scanInterface(iface) {
         if (line.startsWith('DS Parameter set: channel')) {
             partial.channel = line.split(':')[1].trim();
         }
+        if (line.startsWith('* primary channel:')) {
+            partial.channel = 'channel ' + line.split(':')[1].trim();
+        }
         if (line.startsWith('freq: ')) {
             partial.freq = 'freq ' + line.split(':')[1].trim();
         }
     }
 
+    finishPartial();
+
     function finishPartial() {
         if (!partial) {
             return;
@@ -74,22 +84,29 @@ async function scanInterface(iface) {
 
     return results
         .sort()
-        .map(([, {bssid, ssid, signal, channel}]) => {
-            ssid = ssid.padStart(40, ' ');
-            channel = channel.padEnd(10, ' ');
-            return `${signal}  ${channel}  ${ssid}  ${bssid}`;
+        .map(([, {bssid, ssid, signal, channel, associated}]) => {
+            ssid = ssid.padStart(40, ' ').substr(0, 40);
+            channel = channel.padEnd(12, ' ');
+            return `${signal}  ${channel}  ${ssid}  ${bssid}  ${associated}`;
         })
         .join('\n') + '\n';
 }
 
 async function main() {
-    const iface = await findInterface();
-
-    for (;;) {
-        const scanResult = await scanInterface(iface).catch((err) => err.toString());
-        process.stdout.write('\x1b[2J\x1b[0f');
-        process.stdout.write(scanResult);
-        await sleep(3000);
+    const iface = process.argv[2] || await findInterface();
+
+    if (iface === '-') {
+        const scanResult = await fs.readFile('/dev/stdin', 'utf-8');
+        const prettyScanResult = formatScanResult(scanResult);
+        process.stdout.write(prettyScanResult);
+    } else {
+        for (;;) {
+            const scanResult = await scanInterface(iface).catch((err) => err.toString());
+            const prettyScanResult = formatScanResult(scanResult);
+            process.stdout.write('\x1b[2J\x1b[0f');
+            process.stdout.write(prettyScanResult);
+            await sleep(3000);
+        }
     }
 }