OpenWrt: uhttpd服务接收POST数据

在/www 文件夹下,创建lua服务文件。文件路径加文件名即为访问路径

OpenWrt: uhttpd服务接收POST数据_第1张图片
例如query服务访问路径为:http://address/cgi-bin/query

接受POST数据并返回

-- 获取数据长度
POSTLength = tonumber(os.getenv("CONTENT_LENGTH")) or 0
if (POSTLength > 0) then
    POST = io.read(POSTLength)
    -- 处理数据
end

响应请求

io.write("Content-type: text/html\nPragma: no-cache\n\n")
io.write("saved changes,please waiting for the apply\n")

生效配置

/etc/init.d/uhttpd restart

通过http访问修改wrt sta 密码

#!/usr/bin/lua
url = require 'cgi-bin/url'
io.write("Content-type: text/html\nPragma: no-cache\n\n")
s = "success"

out = io.stdout

function wifi_set(map)
    local f = io.popen("uci set wireless.@wifi-iface[-1].device=\"radio0\"")
    f:close()
    f = io.popen("uci set wireless.@wifi-iface[-1].network=\"wan wwan\"")
    f:close()
    f = io.popen("uci set wireless.@wifi-iface[-1].mode=\"sta\"")
    f:close()
    f = io.popen(string.format("uci set wireless.@wifi-iface[-1].encryption=\"%s\"", map["encmethod"] or "psk2"))
    f:close()
    f = io.popen(string.format("uci set wireless.@wifi-iface[-1].key=\"%s\"", map["ssid_pas"] or "123456"))
    f:close()
    f = io.popen(string.format("uci set wireless.@wifi-iface[-1].ssid=\"%s\"", map["ssid"] or "Openwrt"))
    f:close()
end

function wifi_reload()
    local f = io.popen("uci commit wireless")
    f:close()
    f = io.popen("wifi reload")
    f:close()
end

POSTLength = tonumber(os.getenv("CONTENT_LENGTH")) or 0
if (POSTLength > 0) then
    POST = io.read(POSTLength)
    POST = url.parseQuery(POST)
    wifi_set(POST)
end

io.write("saved changes,please waiting for the apply\n")

wifi_reload()

你可能感兴趣的:(openwrt,lua)