pretty-wifi-info.lua (2067B)
1 #!/usr/bin/lua 2 3 local function get_basic_net_info(network, iface, accumulator) 4 local net = network:get_network(iface) 5 local device = net and net:get_interface() 6 7 if device then 8 accumulator["uptime"] = net:uptime() 9 accumulator["iface"] = device:name() 10 accumulator["mac"] = device:mac() 11 accumulator["rx_bytes"] = device:rx_bytes() 12 accumulator["tx_bytes"] = device:tx_bytes() 13 accumulator["ipaddrs"] = {} 14 15 for _, ipaddr in ipairs(device:ipaddrs()) do 16 accumulator.ipaddrs[#accumulator.ipaddrs + 1] = { 17 addr = ipaddr:host():string(), 18 netmask = ipaddr:mask():string() 19 } 20 end 21 end 22 end 23 24 local function get_wifi_info(network, iface, accumulator) 25 local net = network:get_wifinet(iface) 26 27 if net then 28 local dev = net:get_device() 29 if dev then 30 accumulator["mode"] = net:active_mode() 31 accumulator["ssid"] = net:active_ssid() 32 accumulator["encryption"] = net:active_encryption() 33 accumulator["quality"] = net:signal_percent() 34 end 35 end 36 end 37 38 local function collect_wifi_info() 39 local network = require"luci.model.network".init() 40 local accumulator = {} 41 get_basic_net_info(network, "lan", accumulator) 42 get_wifi_info(network, "wlan0", accumulator) 43 return accumulator 44 end 45 46 local info = collect_wifi_info() 47 48 print("Current WiFi configuration") 49 if info.ssid then 50 print("SSID: " .. info.ssid) 51 end 52 if info.mode then 53 print("Mode: " .. info.mode) 54 end 55 if info.quality then 56 print("Signal: " .. info.quality .. "%") 57 end 58 if info.encryption then 59 print("Encryption method: " .. info.encryption) 60 end 61 if info.iface then 62 print("Interface name: " .. info.iface) 63 end 64 if info.uptime then 65 print("Active for: " .. math.floor(info.uptime / 60) .. " minutes") 66 end 67 if #info.ipaddrs > 0 then 68 print("IP address: " .. info.ipaddrs[1].addr .. "/" .. info.ipaddrs[1].netmask) 69 end 70 if info.mac then 71 print("MAC address: " .. info.mac) 72 end 73 if info.rx_bytes and info.tx_bytes then 74 print("RX/TX: " .. math.floor(info.rx_bytes / 1024) .. "/" .. math.floor(info.tx_bytes / 1024) .. " KBs") 75 end