milkymist-files/files/m1-wireless.lua |
1 | | #!/usr/bin/env lua |
2 | | |
3 | | local io = require "io" |
4 | | local os = require "os" |
5 | | local table = require "table" |
6 | | |
7 | | function trim (s) |
8 | | return (string.gsub(s, "^%s*(.-)%s*$", "%1")) |
9 | | end |
10 | | |
11 | | function 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 |
42 | | end |
43 | | |
44 | | function exec(command) |
45 | | local pp = io.popen(command) |
46 | | local data = pp:read("*a") |
47 | | pp:close() |
48 | | |
49 | | return data |
50 | | end |
51 | | |
52 | | function 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 r = Q .. "\t" .. ESSID .. "\t" .. enc |
78 | | |
79 | | table.insert(iws, r) |
80 | | end |
81 | | end |
82 | | |
83 | | return iws |
84 | | end |
85 | | |
86 | | local t = iwscan("wlan0") |
87 | | for k,v in pairs(t) do print(v) end |
milkymist-files/files/m1-wireless.sh |
1 | | #!/bin/sh |
2 | | |
3 | | if [ "$1" == "set" ] && [ "$#" == "4" ]; then |
4 | | uci delete network.wwan |
5 | | uci commit wireless |
6 | | |
7 | | uci set network.wwan=interface |
8 | | uci set network.wwan.proto=dhcp |
9 | | uci commit wireless |
10 | | |
11 | | uci delete wireless.@wifi-iface[0] |
12 | | uci commit wireless |
13 | | |
14 | | uci set wireless.@wifi-device[0].disabled=0 |
15 | | uci add wireless wifi-iface > /dev/null 2>&1 |
16 | | uci set wireless.@wifi-iface[0].device='radio0' |
17 | | uci set wireless.@wifi-iface[0].network='wwan' |
18 | | uci set wireless.@wifi-iface[0].mode='sta' |
19 | | |
20 | | #none for an open network, |
21 | | #wep for WEP, |
22 | | #psk for WPA-PSK, or |
23 | | #psk2 |
24 | | |
25 | | uci set wireless.@wifi-iface[0].ssid=$2 |
26 | | uci set wireless.@wifi-iface[0].key=$3 |
27 | | uci set wireless.@wifi-iface[0].encryption=$4 |
28 | | |
29 | | uci commit wireless |
30 | | wifi up > /dev/null 2>&1 |
31 | | udhcpc -i wlan0 -b > /dev/null 2>&1 |
32 | | |
33 | | ifconfig wlan0 | grep "inet addr" |
34 | | |
35 | | exit 0 |
36 | | fi |
37 | | |
38 | | |
39 | | if [ "$1" == "get" ]; then |
40 | | ifconfig -a | grep wlan0 > /dev/null 2>&1 |
41 | | |
42 | | if [ "$?" != "0" ]; then |
43 | | iw phy phy0 interface add wlan0 type station |
44 | | ifconfig wlan0 up |
45 | | sleep 1 |
46 | | fi |
47 | | |
48 | | /opt/m1-wireless.lua |
49 | | |
50 | | exit 0 |
51 | | fi |
52 | | |
53 | | if [ "$1" == "status" ]; then |
54 | | ifconfig wlan0 |
55 | | |
56 | | exit 0 |
57 | | fi |
58 | | |
59 | | echo "Usage: $0 [get/set/status] PARAMS..." |
milkymist-files/files/opt/m1-wireless.lua |
| 1 | #!/usr/bin/env lua |
| 2 | |
| 3 | local io = require "io" |
| 4 | local os = require "os" |
| 5 | local table = require "table" |
| 6 | |
| 7 | function trim (s) |
| 8 | return (string.gsub(s, "^%s*(.-)%s*$", "%1")) |
| 9 | end |
| 10 | |
| 11 | function 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 |
| 42 | end |
| 43 | |
| 44 | function exec(command) |
| 45 | local pp = io.popen(command) |
| 46 | local data = pp:read("*a") |
| 47 | pp:close() |
| 48 | |
| 49 | return data |
| 50 | end |
| 51 | |
| 52 | function 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 |
| 88 | end |
| 89 | |
| 90 | local t = iwscan("wlan0") |
| 91 | if t == {} then |
| 92 | print(); |
| 93 | else |
| 94 | for k,v in pairs(t) do print(v) end |
| 95 | end |
| 96 | |
milkymist-files/files/opt/m1-wireless.sh |
| 1 | #!/bin/sh |
| 2 | |
| 3 | if [ "$1" == "set" ] && [ "$#" == "4" ]; then |
| 4 | uci delete network.wwan |
| 5 | uci commit wireless |
| 6 | |
| 7 | uci set network.wwan=interface |
| 8 | uci set network.wwan.proto=dhcp |
| 9 | uci commit wireless |
| 10 | |
| 11 | uci delete wireless.@wifi-iface[0] |
| 12 | uci commit wireless |
| 13 | |
| 14 | uci set wireless.@wifi-device[0].disabled=0 |
| 15 | uci add wireless wifi-iface > /dev/null 2>&1 |
| 16 | uci set wireless.@wifi-iface[0].device='radio0' |
| 17 | uci set wireless.@wifi-iface[0].network='wwan' |
| 18 | uci set wireless.@wifi-iface[0].mode='sta' |
| 19 | |
| 20 | #none for an open network, |
| 21 | #wep for WEP, |
| 22 | #psk for WPA-PSK, or |
| 23 | #psk2 |
| 24 | |
| 25 | uci set wireless.@wifi-iface[0].ssid=$2 |
| 26 | uci set wireless.@wifi-iface[0].key=$3 |
| 27 | uci set wireless.@wifi-iface[0].encryption=$4 |
| 28 | |
| 29 | uci commit wireless |
| 30 | wifi up > /dev/null 2>&1 |
| 31 | udhcpc -i wlan0 -b > /dev/null 2>&1 |
| 32 | |
| 33 | ifconfig wlan0 | grep "inet addr" |
| 34 | |
| 35 | exit 0 |
| 36 | fi |
| 37 | |
| 38 | |
| 39 | if [ "$1" == "get" ]; then |
| 40 | ifconfig -a | grep wlan0 > /dev/null 2>&1 |
| 41 | |
| 42 | if [ "$?" != "0" ]; then |
| 43 | iw phy phy0 interface add wlan0 type station |
| 44 | ifconfig wlan0 up |
| 45 | sleep 1 |
| 46 | fi |
| 47 | |
| 48 | /opt/m1-wireless.lua |
| 49 | |
| 50 | exit 0 |
| 51 | fi |
| 52 | |
| 53 | if [ "$1" == "status" ]; then |
| 54 | sleep 1 |
| 55 | IP=`ifconfig wlan0 2>/dev/null | grep "inet addr" | \ |
| 56 | sed -e 's/^ *//g' | cut -d":" -f2 | cut -d" " -f1` |
| 57 | if [ "$?" != 0 ] || [ "${IP}" == "" ]; then |
| 58 | echo "Disconnected" |
| 59 | echo "0.0.0.0" |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | ESSID=`iwconfig wlan0 2>/dev/null | grep ESSID | cut -d":" -f 2 | sed -e 's/\"//g'` |
| 64 | |
| 65 | echo "${ESSID}" |
| 66 | echo "${IP}" |
| 67 | |
| 68 | exit 0 |
| 69 | fi |
| 70 | |
| 71 | echo "Usage: $0 [get/set/status] PARAMS..." |
| 72 | exit 1 |