• WAP手机版 保存到桌面加入收藏设为首页
路由器

修改超链--为你的luci添加自助高级配置界面(含源码)

时间:2016-05-29 10:16:09   作者:哎丫丫转载   阅读:1611   评论:0
内容摘要:想到,如果可以在luci界面编辑一下该多好,有了idea,就行动起来,下面是详细教程:第一部分:添加一个luci界面(给新手看的)添加自己的luci界面...


因为经常要自己进去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文档的内容如下:
  1. config abcdefg
复制代码
/usr/lib/lua/luci/controller/abcdefg.lua文档内容如下:(b)
  1. function index()
  2.         if not nixio.fs.access("/etc/config/abcdefg") then
  3.                 return
  4.         end
  5.         entry({"admin", "system", "abcdefg"}, cbi("abcdefg"), _("高级配置")).dependent = true
  6. end
复制代码
/usr/lib/lua/luci/model/cbi/abcdefg.lua文档内容如下:(c)
  1. --teasiu<teasiu@163.com>
  2. local fs = require "nixio.fs"
  3. local sys = require "luci.sys"
  4. m = Map("abcdefg", translate("openwrt高级设置"), translate("各类服务内置脚本文档的直接编辑,除非你知道自己在干什么,否则请不要轻易修改这些配置文档"))
  5. s = m:section(TypedSection, "abcdefg")
  6. s.anonymous=true
  7. --这里开始添加东西
  8. return m
复制代码
ok,到这里,把3个文档分别传到路由器相应的位置,你就建立了一个属于你自己的luci界面了。


第二部分:添加你需要-功能
上面的框架搭好了,接下来,我们就在c文档添加自己需要的功能了:
首先小试牛刀,添加一个修改固件版本的文档/etc/openwrt_release看看
  1. --teasiu<teasiu@163.com>
  2. local fs = require "nixio.fs"
  3. local sys = require "luci.sys"
  4. m = Map("abcdefg", translate("openwrt高级设置"), translate("各类服务内置脚本文档的直接编辑,除非你知道自己在干什么,否则请不要轻易修改这些配置文 "))
  5. s = m:section(TypedSection, "abcdefg")
  6. s.anonymous=true
  7. --这里开始添加东西
  8. s:tab("config", translate("固件版本"),translate("修改成你喜欢看到的名字"))
  9. conf = s:taboption("config", Value, "editconf", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  10. conf.template = "cbi/tvalue"
  11. conf.rows = 20
  12. conf.wrap = "off"
  13. function conf.cfgvalue(self, section)
  14.         return fs.readfile("/etc/openwrt_release") or ""
  15. end
  16. function conf.write(self, section, value)
  17.         if value then
  18.                 value = value:gsub("\r\n?", "\n")
  19.                 fs.writefile("/tmp/openwrt_release", value)
  20.                 if (luci.sys.call("cmp -s /tmp/openwrt_release /etc/openwrt_release") == 1) then
  21.                         fs.writefile("/etc/openwrt_release", value)
  22.                 end
  23.                 fs.remove("/tmp/openwrt_release")
  24.         end
  25. end
  26. --这里结束添加东西
  27. --这里继续添加东西
  28. return m
复制代码
到了这里,如果你把这c文档都传上去路由器覆盖一下,你应该看到一个新的页面包含了编辑文档/etc/openwrt_release的功能了。

就一个?当然不满足啦,我们说好了把想要的文档编辑都实现界面化呢?那你就需要继续往下看了:

第三部分:进阶部分
到了这里,相信你已经是一个有能力改代码的淫了,我就一股脑把我自己的代码贴上来,
你自己看着需求增减吧:
  1. --teasiu<teasiu@163.com>
  2. local fs = require "nixio.fs"
  3. local sys = require "luci.sys"
  4. m = Map("abcdefg", translate("openwrt高级设置"), translate("各类服务内置脚本文档的直接编辑,除非你知道自己在干什么,否则请不要轻易修改这些配置文档"))
  5. s = m:section(TypedSection, "abcdefg")
  6. s.anonymous=true
  7. --这里开始添加东西
  8. s:tab("config", translate("固件版本"),translate("修改成你喜欢看到的名字"))
  9. conf = s:taboption("config", Value, "editconf", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  10. conf.template = "cbi/tvalue"
  11. conf.rows = 20
  12. conf.wrap = "off"
  13. function conf.cfgvalue(self, section)
  14. return fs.readfile("/etc/openwrt_release") or ""
  15. end
  16. function conf.write(self, section, value)
  17. if value then
  18. value = value:gsub("\r\n?", "\n")
  19. fs.writefile("/tmp/openwrt_release", value)
  20. if (luci.sys.call("cmp -s /tmp/openwrt_release /etc/openwrt_release") == 1) then
  21. fs.writefile("/etc/openwrt_release", value)
  22. end
  23. fs.remove("/tmp/openwrt_release")
  24. end
  25. end
  26. --这里结束添加东西
  27. --这里继续添加东西
  28. if nixio.fs.access("/etc/exports") then  --这里有if的条件判断,就是说你的路由器如果没有这个功能,这一段是不会显示的
  29. if sys.call("pidof nfsd >/dev/null") == 0 then
  30. 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>"))
  31. else
  32. 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>"))
  33. end
  34. conf = s:taboption("config2", Value, "editconf2", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  35. conf.template = "cbi/tvalue"
  36. conf.rows = 20
  37. conf.wrap = "off"
  38. function conf.cfgvalue(self, section)
  39.     return fs.readfile("/etc/exports") or ""
  40. end
  41. function conf.write(self, section, value)
  42.     if value then
  43.         value = value:gsub("\r\n?", "\n")
  44.         fs.writefile("/tmp/exports", value)
  45.         if (luci.sys.call("cmp -s /tmp/exports /etc/exports") == 1) then
  46.             fs.writefile("/etc/exports", value)
  47.             luci.sys.call("exportfs -r >/dev/null")  --这里添加了一个命令,让你保存应用后,nfsd会立即生效。
  48.         end
  49.         fs.remove("/tmp/exports")
  50.     end
  51. end
  52. end
  53. --
  54. --
  55. --
  56. if nixio.fs.access("/etc/vsftpd.conf") then
  57. if sys.call("pidof vsftpd >/dev/null") == 0 then
  58. s:tab("config3", translate("配置FTP"),translate("FTP服务器运行中,本页是配置/etc/vsftpd.conf的文档内容"))
  59. else
  60. s:tab("config3", translate("配置FTP"),translate("FTP服务器未运行,本页是配置/etc/vsftpd.conf的文档内容"))
  61. end
  62. conf = s:taboption("config3", Value, "editconf3", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  63. conf.template = "cbi/tvalue"
  64. conf.rows = 20
  65. conf.wrap = "off"
  66. function conf.cfgvalue(self, section)
  67.     return fs.readfile("/etc/vsftpd.conf") or ""
  68. end
  69. function conf.write(self, section, value)
  70.     if value then
  71.         value = value:gsub("\r\n?", "\n")
  72.         fs.writefile("/tmp/vsftpd.conf", value)
  73.         if (luci.sys.call("cmp -s /tmp/vsftpd.conf /etc/vsftpd.conf") == 1) then
  74.             fs.writefile("/etc/vsftpd.conf", value)
  75.         end
  76.         fs.remove("/tmp/vsftpd.conf")
  77.     end
  78. end
  79. end
  80. --
  81. --
  82. --
  83. if nixio.fs.access("/etc/dnsmasq.conf") then
  84. s:tab("config4", translate("配置dnsmasq"),translate("本页是配置/etc/dnsmasq.conf的文档内容。应用保存后重启生效"))
  85. conf = s:taboption("config4", Value, "editconf4", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  86. conf.template = "cbi/tvalue"
  87. conf.rows = 20
  88. conf.wrap = "off"
  89. function conf.cfgvalue(self, section)
  90.     return fs.readfile("/etc/dnsmasq.conf") or ""
  91. end
  92. function conf.write(self, section, value)
  93.     if value then
  94.         value = value:gsub("\r\n?", "\n")
  95.         fs.writefile("/tmp/dnsmasq.conf", value)
  96.         if (luci.sys.call("cmp -s /tmp/dnsmasq.conf /etc/dnsmasq.conf") == 1) then
  97.             fs.writefile("/etc/dnsmasq.conf", value)
  98.             luci.sys.call("/etc/init.d/dnsmasq restart >/dev/null")  --这里增加了一条重启dnsmasq的命令
  99.         end
  100.         fs.remove("/tmp/dnsmasq.conf")
  101.     end
  102. end
  103. end
  104. --
  105. --
  106. --
  107. if nixio.fs.access("/etc/wifidog.conf") then
  108. s:tab("config5", translate("配置wifidog"),translate("本页是配置/etc/wifidog.conf的文档内容。"))
  109. conf = s:taboption("config5", Value, "editconf5", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  110. conf.template = "cbi/tvalue"
  111. conf.rows = 20
  112. conf.wrap = "off"
  113. function conf.cfgvalue(self, section)
  114.     return fs.readfile("/etc/wifidog.conf") or ""
  115. end
  116. function conf.write(self, section, value)
  117.     if value then
  118.         value = value:gsub("\r\n?", "\n")
  119.         fs.writefile("/tmp/wifidog.conf", value)
  120.         if (luci.sys.call("cmp -s /tmp/wifidog.conf /etc/wifidog.conf") == 1) then
  121.             fs.writefile("/etc/wifidog.conf", value)
  122.         end
  123.         fs.remove("/tmp/wifidog.conf")
  124.     end
  125. end
  126. end
  127. --
  128. --
  129. --
  130. if nixio.fs.access("/etc/config/network") then
  131. s:tab("config6", translate("配置network"),translate("本页是配置/etc/config/network的文档内容。<a href=\"https://wiki.openwrt.org/doc/howto/start\" target=\"_blank\">  教程>></a>"))
  132. conf = s:taboption("config6", Value, "editconf6", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  133. conf.template = "cbi/tvalue"
  134. conf.rows = 20
  135. conf.wrap = "off"
  136. function conf.cfgvalue(self, section)
  137.     return fs.readfile("/etc/config/network") or ""
  138. end
  139. function conf.write(self, section, value)
  140.     if value then
  141.         value = value:gsub("\r\n?", "\n")
  142.         fs.writefile("/tmp/netwok", value)
  143.         if (luci.sys.call("cmp -s /tmp/network /etc/config/network") == 1) then
  144.             fs.writefile("/etc/config/network", value)
  145.             luci.sys.call("/etc/init.d/network restart >/dev/null")
  146.         end
  147.         fs.remove("/tmp/network")
  148.     end
  149. end
  150. end
  151. --
  152. --
  153. --
  154. if nixio.fs.access("/etc/phlinux.conf") then
  155. if sys.call("pidof phddns >/dev/null") == 0 then
  156. s:tab("config7", translate("配置花生壳"),translate("检测到花生壳服务已经运行中,本页是配置/etc/phlinux.conf的文档内容。<a href=\"http://service.oray.com/question/116.html\" target=\"_blank\">  教程>></a>"))
  157. else
  158. s:tab("config7", translate("配置花生壳"),translate("检测到花生壳服务尚未运行,本页是配置/etc/phlinux.conf的文档内容。<a href=\"http://service.oray.com/question/116.html\" target=\"_blank\">  教程>></a>"))
  159. end
  160. conf = s:taboption("config7", Value, "editconf7", nil, translate("开头的数字符号(#)或分号的每一行(;)被视为注释;删除(;)启用指定选项。"))
  161. conf.template = "cbi/tvalue"
  162. conf.rows = 10
  163. conf.wrap = "off"
  164. function conf.cfgvalue(self, section)
  165.     return fs.readfile("/etc/phlinux.conf") or ""
  166. end
  167. function conf.write(self, section, value)
  168.     if value then
  169.         value = value:gsub("\r\n?", "\n")
  170.         fs.writefile("/tmp/phlinux.conf", value)
  171.         if (luci.sys.call("cmp -s /tmp/phlinux.conf /etc/phlinux.conf") == 1) then
  172.             fs.writefile("/etc/phlinux.conf", value)
  173.             luci.sys.call("/usr/bin/phddns -c /etc/phlinux.conf -d >/dev/null")
  174.         end
  175.         fs.remove("/tmp/phlinux.conf")
  176.     end
  177. end
  178. end

  179. 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文档:
代码如下:
  1. include $(TOPDIR)/rules.mk

  2. LUCI_TITLE:=LuCI Support for mynameabc
  3. LUCI_DEPENDS:=

  4. include ../../luci.mk

  5. # call BuildPackage - OpenWrt buildroot signature
复制代码
接下来
./scripts/feeds update luci
./scripts/feeds install -a -p luci
然后make menuconfig
你就能在luci的app下找到并添加了。
   版权声明,所有转载都有注明出处,本站不负责承担任何法律责往。若有侵权,请联系我。我会及时删除。

电脑维护,系统安装,软 、硬件维修,电脑配件,零售业务,网站建设,路由器安装设置 服务器维护,电脑、网络维护,智能手机刷机,安装WIFI 调试!郴州网站建设 小程序搭建 郴州电脑维修

        咨询电话:18175576644  点击这里给我发消 息
   扫描二维码。关注公众号,小程序
       享受星级服务   

手机点击二维码关注
      


手机点击打开小程序
      


相关评论
免责申明:本站部分资料来源互联网,如果侵犯了您的版权,请作者速来电或QQ与本站联系,我们将第一时间给予以改正或删除。

Copyright © 2020 哎丫丫电脑 All Rights Reserved 
 工信部备案:湘ICP备14010293号-1