mt7688 openwrt之lua初探

Openwrt操作无线接口有很多值得借鉴地方,如获取dhcp列表,获取无线列表,获取连接流量等。而这一切依靠Lua


背景:需要通过iw获取无线列表,并根据无线加密类型进行连接。参考openwrt原生界面:


1. 安装lua
官网默认镜像居然没有lua的支持,那么安装之:
opkg install luci
/etc/init.d/uhttpd start


2. lua目录相关信息;
/usr/lib/lua/luci/controller/  controller实现目录
/usr/lib/lua/luci/model/cbi/admin_network/wifi_add.lua    这里应该是操作iw的文件


一些对应关系:
一番搜索后发现此页面的源码在系统的/usr/lib/lua/luci/view/admin_network/wifi_join.htm文件里,对应openwrt的源码路径
为./feeds/luci/modules/admin-full/luasrc/view/admin_network/wifi_join.htm。






由local iw = luci.sys.wifi.getiwinfo(dev)找到sys.lua模块,路径/usr/lib/lua/luci/sys.lua,对应openwrt源码位置./feeds/luci/modules/base/luasrc/sys.lua


root@OpenWrt:/usr/lib/lua/luci# grep "scanlist" * -r
view/admin_network/wifi_join.htm:       function scanlist(times)
view/admin_network/wifi_join.htm:                       for k, v in ipairs(iw.scanlist or { }) do
view/admin_network/wifi_join.htm:                       <% for i, net in ipairs(scanlist(3)) do net.encryption = net.encryption or { } %>
root@OpenWrt:/usr/lib/lua/luci#


root@OpenWrt:/usr/lib/lua# grep "scanlist" * -r
iwinfo.so:scanlist


调试思路:
https://my.oschina.net/osbin/blog/298526


目前openwrt具体实现:


function format_wifi_encryption(info)
if info.wep == true then
return "WEP"
elseif info.wpa > 0 then
return translatef("%s - %s",
table.concat(info.pair_ciphers, ", "),
table.concat(info.group_ciphers, ", "),
(info.wpa == 3) and translate("mixed WPA/WPA2")
or (info.wpa == 2 and "WPA2" or "WPA"),
table.concat(info.auth_suites, ", ")
)
elseif info.enabled then
return "%s" % translate("unknown")
else
return "%s" % translate("open")
end
end


网友板:
#!/usr/bin/lua


dev = arg[1]
local sys = require "luci.sys"
local utl = require "luci.util"
local iw = luci.sys.wifi.getiwinfo(dev)


    function scanlist(times)
        local i, k, v
        local l = { }
        local s = { }


        for i = 1, times do
            for k, v in ipairs(iw.scanlist or { }) do
                if not s[v.bssid] then
                    l[#l+1] = v
                    s[v.bssid] = true
                end
            end
        end


        return l
    end


    function format_wifi_encryption(info)
        if info.wep == true then
            return "WEP"
        elseif info.wpa > 0 then
            return string.format("Pairwise: %s / Group: %s >%s - %s",
                table.concat(info.pair_ciphers, ", "),
                table.concat(info.group_ciphers, ", "),
                (info.wpa == 3) and string.format("mixed WPA/WPA2")
                    or (info.wpa == 2 and "WPA2" or "WPA"),
                table.concat(info.auth_suites, ", ")
            )
        elseif info.enabled then
            return unknown
        else
            return open
        end
    end




for i, net in ipairs(scanlist(3)) do
    net.encryption = net.encryption or { }
    print("channel:",net.channel);
    print("ssid:",net.ssid);
    print("bssid:",net.bssid);
    print("Mode:",net.mode);
    wep=net.encryption.wep and 1 or 0
    
    print("Encryption:",format_wifi_encryption(net.encryption));
    print("");
end

你可能感兴趣的:(mt7688 openwrt之lua初探)