Root/milkymist-TLWR703/files/opt/m1-wireless.lua

1#!/usr/bin/env lua
2
3local io = require "io"
4local os = require "os"
5local table = require "table"
6
7function trim (s)
8    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
9end
10
11function split(delim, text)
12    local list = {}
13    if string.len(text) <= 0 then
14       return list
15    end
16    delim = delim or ""
17    local pos = 1
18    -- if delim matches empty string then it would give an endless loop
19    if string.find("", delim, 1) and delim ~= "" then
20       error("delim matches empty string!")
21    end
22    local first, last
23    while 1 do
24       if delim ~= "" then
25          first, last = string.find(text, delim, pos)
26       else
27          first, last = string.find(text, "%s+", pos)
28          if first == 1 then
29         pos = last+1
30         first, last = string.find(text, "%s+", pos)
31          end
32       end
33       if first then -- found?
34          table.insert(list, string.sub(text, pos, first-1))
35          pos = last+1
36       else
37          table.insert(list, string.sub(text, pos))
38          break
39       end
40    end
41    return list
42end
43
44function exec(command)
45        local pp = io.popen(command)
46        local data = pp:read("*a")
47    pp:close()
48
49        return data
50end
51
52function iwscan(iface)
53    local siface = iface or ""
54    local cnt = exec("iwlist "..siface.." scan 2>/dev/null")
55    local iws = {}
56
57    cnt = trim(cnt)
58    for i, l in pairs(split("\n Cell", cnt)) do
59       local ESSID = l:match("ESSID:\"(.-)\"")
60       if ESSID then
61          local Q = l:match("Quality[:=](.-)[ ]")
62          local A = l:match("Authentication Suites.- : (.-)\n")
63          local P = l:match("Pairwise Ciphers.- : (.-)\n")
64          
65          local enc
66          if not l:find("Encryption key:on") then
67         enc = "none"
68          else
69         local wpa = l:find("WPA Version 1")
70         local rsn = l:find("WPA2 Version 1")
71         if wpa and rsn then enc = "psk+psk2"
72         elseif rsn then enc = "psk2"
73         elseif wpa then enc = "psk"
74         else enc = "wep" end
75          end
76          
77          local a = Q:match("(%d-)/")
78          local b = Q:match("/(%d-)$")
79          Q = math.floor(tonumber(a)/tonumber(b)*100)
80
81          local r = Q .. "%," .. ESSID .. "," .. enc
82
83          table.insert(iws, r)
84       end
85    end
86    
87    return iws
88end
89
90local t = iwscan("wlan0")
91if t == {} then
92    print();
93else
94    for k,v in pairs(t) do print(v) end
95end
96        
97

Archive Download this file



interactive