Advertisement
Guest User

Untitled

a guest
Jun 12th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. root@uplink:~# cat /tmp/iwinfo-json.lua
  2. #!/usr/bin/lua
  3.  
  4. local iw = require "iwinfo"
  5. local dev = arg[1] or "wlan0"
  6.  
  7. function write_json(x)
  8. if x == nil then
  9. io.write("null")
  10. elseif type(x) == "table" then
  11. local k, v
  12. if type(next(x)) == "number" then
  13. io.write("[ ")
  14. for k, v in ipairs(x) do
  15. write_json(v)
  16. if next(x, k) then
  17. io.write(", ")
  18. end
  19. end
  20. io.write(" ]")
  21. else
  22. io.write("{ ")
  23. for k, v in pairs(x) do
  24. io.write(string.format("%q: ", k))
  25. write_json(v)
  26. if next(x, k) then
  27. io.write(", ")
  28. end
  29. end
  30. io.write(" }")
  31. end
  32. elseif type(x) == "number" or type(x) == "boolean" then
  33. if (x ~= x) then
  34. -- NaN is the only value that doesn't equal to itself.
  35. io.write("Number.NaN")
  36. else
  37. io.write(tostring(x))
  38. end
  39. else
  40. io.write(string.format('"%s"', tostring(x):gsub('["%z\1-\31]', function(c)
  41. return '\u%04x' % c:byte(1)
  42. end)))
  43. end
  44. end
  45.  
  46.  
  47. local backend = iw.type(dev)
  48. if not backend then
  49. error("No such wireless device:", dev)
  50. end
  51.  
  52. local info = { }
  53. local _, field
  54.  
  55. for _, field in ipairs({
  56. "ssid", "bssid", "mode", "channel", "frequency",
  57. "txpower", "quality", "quality_max",
  58. "signal", "noise", "bitrate", "encryption",
  59. "hwmodelist", "assoclist"
  60. }) do
  61. info[field] = iw[backend][field](dev)
  62. end
  63.  
  64. write_json(info)
  65.  
  66. root@uplink:~# lua /tmp/iwinfo-json.lua wlan1
  67. { "quality_max": 70, "ssid": "Funkueberwachungswagen 5", "encryption": { "enabled": true, "auth_algs": [ "OPEN" ], "description": "WPA2 PSK (CCMP)", "wep": false, "auth_suites": [ "PSK" ], "wpa": 2, "pair_ciphers": [ "CCMP" ], "group_ciphers": [ "CCMP" ] }, "assoclist": { "00:15:61:10:51:DF": { "rx_short_gi": true, "noise": -95, "rx_mcs": 5, "tx_40mhz": true, "rx_40mhz": true, "tx_rate": 108000, "tx_packets": 739361, "tx_short_gi": false, "rx_packets": 402970, "tx_mcs": 11, "inactive": 2060, "rx_rate": 120000, "signal": -75 } }, "bssid": "C0:3F:0E:7A:A0:96", "hwmodelist": { "a": true, "n": true, "g": false, "b": false }, "bitrate": 108000, "txpower": 17, "noise": -95, "channel": 149, "signal": -75, "mode": "Master", "quality": 35, "frequency": 5745 }
  68. root@uplink:~#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement