lua 修改密码基本设置

这篇博客介绍了如何使用 Lua 脚本在 LuCI 界面中实现管理员密码的修改。通过调用 `luci.http.formvalue` 获取输入的密码,使用 MD5 加密密码,并更新配置文件来完成修改。在密码不匹配或未提供时,脚本会进行相应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

module("luci.controller.web", package.seeall)
function index()
local fs = require "nixio.fs"
entry({"admin", "system","web"}, call("action"), "修改密码", 6)
entry({"admin", "system","web","commit"}, call("commit"))
end
--设置修改密码向导页面
function action()
luci.template.render("admin_system/web")
end
 --修改密码基本设置
function commit()
local uci = require "luci.model.uci".cursor()
local v1 = luci.http.formvalue("pw1")
local v2 = luci.http.formvalue("pw2")
if v1=="" and v2=="" then 
--os.execute("/etc/init.d/dropbear restart")
luci.sys.exec("/etc/init.d/dropbear restart")
end 
if v1 and v2 and #v1 > 0 and #v2 > 0 then
if v1 == v2 then
--[[if luci.sys.user.setpasswd(luci.dispatcher.context.authuser, v1) == 0 then
m.message = translate("Password successfully changed!")
else
m.message = translate("Unknown Error, password not changed!")
end--]]   
  --md5加密
  md5pass = luci.sys.exec("echo -n \""..v1.."\"|md5sum|cut -d ' ' -f1")   
  md5pass=string.gsub(md5pass,"\n","")
  --修改配置文件里面的密码 
  --luci.sys.exec("uci set web.@web[0].passwd="..md5pass)
  --luci.sys.exec("uci commit web 2>/dev/null")
  local uci = require "luci.model.uci".cursor()
--[[uci:foreach("web","web",function(s)
  local lcName = s[".name"]
  uci:set("web",lcName,"passwd",md5pass)
end
)--]]
uci:set("web","login","passwd",md5pass)
uci:commit("web")
-- m.message = translate("Password successfully changed!")
-- else
-- m.message = translate("Given password confirmation did not match, password not changed!")
end
end
end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值