因为经常要自己进去vi编辑某些配置文档,忽然想到,如果可以在luci界面编辑一下该多好,
有了idea,就
行动起来,下面是详细教程:
第一部分:添加一个luci界面(给新手看的)
添加自己的luci界面,有3个必要的要素(新建文档):
a.新建一个在/etc/config/abcdefg文档
b.新建一个在/usr/lib/lua/luci/controller/abcdefg.lua文档
c.新建一个在/usr/lib/lua/luci/model/cbi/abcdefg.lua文档
它们的作用关系是:b文档是让c文档在luci的菜单中显示出来,c文档是内容和脚本,a文档是c文档定义的变量,一切的主体是c文档。
/etc/config/abcdefg文档的内容如下:
/usr/lib/lua/luci/controller/abcdefg.lua文档内容如下:(b)
-
function index()
-
if not nixio.fs.access("/etc/config/abcdefg") then
-
return
-
end
-
entry({"admin", "system", "abcdefg"}, cbi("abcdefg"), _("高级配置")).dependent = true
-
end
复制代码
/usr/lib/lua/luci/model/cbi/abcdefg.lua文档内容如下:(c)
-
--teasiu<teasiu@163.com>
-
local fs = require "nixio.fs"
-
local sys = require "luci.sys"
-
m = Map("abcdefg", translate("openwrt高级设置"), translate("各类服务内置脚本文档的直接编辑,除非你知道自己在干什么,否则请不要轻易修改这些配置文档"))
-
s = m:section(TypedSection, "abcdefg")
-
s.anonymous=true
-
--这里开始添加东西
-
return m
复制代码
ok,到这里,把3个文档分别传到路由器相应的位置,你就建立了一个属于你自己的luci界面了。
第二部分:添加你需要-功能
上面的框架搭好了,接下来,我们就在c文档添加自己需要的功能了:
首先小试牛刀,添加一个修改固件版本的文档/etc/openwrt_release看看
-
--teasiu<teasiu@163.com>
-
local fs = require "nixio.fs"
-
local sys = require "luci.sys"
-
m = Map("abcdefg", translate("openwrt高级设置"), translate("各类服务内置脚本文档的直接编辑,除非你知道自己在干什么,否则请不要轻易修改这些配置文 "))
-
s = m:section(TypedSection, "abcdefg")
-
s.anonymous=true
-
--这里开始添加东西
-
s:tab("config", translate("固件版本"),translate("修改成你喜欢看到的名字"))
-
conf = s:taboption("config", Value, "editconf", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/openwrt_release") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/openwrt_release", value)
-
if (luci.sys.call("cmp -s /tmp/openwrt_release /etc/openwrt_release") == 1) then
-
fs.writefile("/etc/openwrt_release", value)
-
end
-
fs.remove("/tmp/openwrt_release")
-
end
-
end
-
--这里结束添加东西
-
--这里继续添加东西
-
return m
-
复制代码
到了这里,如果你把这c文档都传上去路由器覆盖一下,你应该看到一个新的页面包含了编辑文档/etc/openwrt_release的功能了。
就一个?当然不满足啦,我们说好了把想要的文档编辑都实现界面化呢?那你就需要继续往下看了:
第三部分:进阶部分
到了这里,相信你已经是一个有能力改代码的淫了,我就一股脑把我自己的代码贴上来,
你自己看着需求增减吧:
-
--teasiu<teasiu@163.com>
-
local fs = require "nixio.fs"
-
local sys = require "luci.sys"
-
m = Map("abcdefg", translate("openwrt高级设置"), translate("各类服务内置脚本文档的直接编辑,除非你知道自己在干什么,否则请不要轻易修改这些配置文档"))
-
s = m:section(TypedSection, "abcdefg")
-
s.anonymous=true
-
--这里开始添加东西
-
s:tab("config", translate("固件版本"),translate("修改成你喜欢看到的名字"))
-
conf = s:taboption("config", Value, "editconf", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/openwrt_release") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/openwrt_release", value)
-
if (luci.sys.call("cmp -s /tmp/openwrt_release /etc/openwrt_release") == 1) then
-
fs.writefile("/etc/openwrt_release", value)
-
end
-
fs.remove("/tmp/openwrt_release")
-
end
-
end
-
--这里结束添加东西
-
--这里继续添加东西
-
if nixio.fs.access("/etc/exports") then --这里有if的条件判断,就是说你的路由器如果没有这个功能,这一段是不会显示的
-
if sys.call("pidof nfsd >/dev/null") == 0 then
-
s:tab("config2", translate("配置NFSD"),translate("nfs服务端运行中,本页是配置/etc/exports的文档内容。<a href=\"http://www.right.com.cn/Forum/thread-182695-1-1.html\" target=\"_blank\"> 教程1>></a>,<a href=\"https://wiki.openwrt.org/doc/howto/nfs.server\" target=\"_blank\"> 教程2>></a>"))
-
else
-
s:tab("config2", translate("配置NFSD"),translate("nfs服务端尚未运行,本页是配置/etc/exports的文档内容。<a href=\"http://www.right.com.cn/Forum/thread-182695-1-1.html\" target=\"_blank\"> 教程1>></a>,<a href=\"https://wiki.openwrt.org/doc/howto/nfs.server\" target=\"_blank\"> 教程2>></a>"))
-
end
-
conf = s:taboption("config2", Value, "editconf2", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/exports") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/exports", value)
-
if (luci.sys.call("cmp -s /tmp/exports /etc/exports") == 1) then
-
fs.writefile("/etc/exports", value)
-
luci.sys.call("exportfs -r >/dev/null") --这里添加了一个命令,让你保存应用后,nfsd会立即生效。
-
end
-
fs.remove("/tmp/exports")
-
end
-
end
-
end
-
--
-
--
-
--
-
if nixio.fs.access("/etc/vsftpd.conf") then
-
if sys.call("pidof vsftpd >/dev/null") == 0 then
-
s:tab("config3", translate("配置FTP"),translate("FTP服务器运行中,本页是配置/etc/vsftpd.conf的文档内容"))
-
else
-
s:tab("config3", translate("配置FTP"),translate("FTP服务器未运行,本页是配置/etc/vsftpd.conf的文档内容"))
-
end
-
conf = s:taboption("config3", Value, "editconf3", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/vsftpd.conf") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/vsftpd.conf", value)
-
if (luci.sys.call("cmp -s /tmp/vsftpd.conf /etc/vsftpd.conf") == 1) then
-
fs.writefile("/etc/vsftpd.conf", value)
-
end
-
fs.remove("/tmp/vsftpd.conf")
-
end
-
end
-
end
-
--
-
--
-
--
-
if nixio.fs.access("/etc/dnsmasq.conf") then
-
s:tab("config4", translate("配置dnsmasq"),translate("本页是配置/etc/dnsmasq.conf的文档内容。应用保存后重启生效"))
-
conf = s:taboption("config4", Value, "editconf4", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/dnsmasq.conf") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/dnsmasq.conf", value)
-
if (luci.sys.call("cmp -s /tmp/dnsmasq.conf /etc/dnsmasq.conf") == 1) then
-
fs.writefile("/etc/dnsmasq.conf", value)
-
luci.sys.call("/etc/init.d/dnsmasq restart >/dev/null") --这里增加了一条重启dnsmasq的命令
-
end
-
fs.remove("/tmp/dnsmasq.conf")
-
end
-
end
-
end
-
--
-
--
-
--
-
if nixio.fs.access("/etc/wifidog.conf") then
-
s:tab("config5", translate("配置wifidog"),translate("本页是配置/etc/wifidog.conf的文档内容。"))
-
conf = s:taboption("config5", Value, "editconf5", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/wifidog.conf") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/wifidog.conf", value)
-
if (luci.sys.call("cmp -s /tmp/wifidog.conf /etc/wifidog.conf") == 1) then
-
fs.writefile("/etc/wifidog.conf", value)
-
end
-
fs.remove("/tmp/wifidog.conf")
-
end
-
end
-
end
-
--
-
--
-
--
-
if nixio.fs.access("/etc/config/network") then
-
s:tab("config6", translate("配置network"),translate("本页是配置/etc/config/network的文档内容。<a href=\"https://wiki.openwrt.org/doc/howto/start\" target=\"_blank\"> 教程>></a>"))
-
conf = s:taboption("config6", Value, "editconf6", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 20
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/config/network") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/netwok", value)
-
if (luci.sys.call("cmp -s /tmp/network /etc/config/network") == 1) then
-
fs.writefile("/etc/config/network", value)
-
luci.sys.call("/etc/init.d/network restart >/dev/null")
-
end
-
fs.remove("/tmp/network")
-
end
-
end
-
end
-
--
-
--
-
--
-
if nixio.fs.access("/etc/phlinux.conf") then
-
if sys.call("pidof phddns >/dev/null") == 0 then
-
s:tab("config7", translate("配置花生壳"),translate("检测到花生壳服务已经运行中,本页是配置/etc/phlinux.conf的文档内容。<a href=\"http://service.oray.com/question/116.html\" target=\"_blank\"> 教程>></a>"))
-
else
-
s:tab("config7", translate("配置花生壳"),translate("检测到花生壳服务尚未运行,本页是配置/etc/phlinux.conf的文档内容。<a href=\"http://service.oray.com/question/116.html\" target=\"_blank\"> 教程>></a>"))
-
end
-
conf = s:taboption("config7", Value, "editconf7", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
-
conf.template = "cbi/tvalue"
-
conf.rows = 10
-
conf.wrap = "off"
-
function conf.cfgvalue(self, section)
-
return fs.readfile("/etc/phlinux.conf") or ""
-
end
-
function conf.write(self, section, value)
-
if value then
-
value = value:gsub("\r\n?", "\n")
-
fs.writefile("/tmp/phlinux.conf", value)
-
if (luci.sys.call("cmp -s /tmp/phlinux.conf /etc/phlinux.conf") == 1) then
-
fs.writefile("/etc/phlinux.conf", value)
-
luci.sys.call("/usr/bin/phddns -c /etc/phlinux.conf -d >/dev/null")
-
end
-
fs.remove("/tmp/phlinux.conf")
-
end
-
end
-
end
-
-
return m
-
-
覆盖c文档,看到的效果是这样的:
第四部分:利用上面的源码编译成ipk,以cc为例:
在feeds/luci/applications下面新建一个
luci-app-myabc文件夹,
mkdir -p feeds/luci/applications/
luci-app-myabc/root/etc/config #这里放a
mkdir -p feeds/luci/applications/
luci-app-myabc/luasrc/controller #这里放b
mkdir -p feeds/luci/applications/
luci-app-myabc/luasrc/model/cbi #这里放c
在luci-app-myabc文件夹下新建一个Makefile文档:
代码如下:
-
include $(TOPDIR)/rules.mk
-
-
LUCI_TITLE:=LuCI Support for mynameabc
-
LUCI_DEPENDS:=
-
-
include ../../luci.mk
-
-
# call BuildPackage - OpenWrt buildroot signature
复制代码
接下来
./scripts/feeds update luci
./scripts/feeds install -a -p luci
然后make menuconfig
你就能在luci的app下找到并添加了。