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("<abbr title='Pairwise: %s / Group: %s'>%s - %s</abbr>",
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 "<em>%s</em>" % translate("unknown")
else
return "<em>%s</em>" % 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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值