openwrt uci系统语法

在UCI的配置文件通常包含一个或多个配置语句,包含一个或多个用来定义实际值的选项语句的所谓的节。

下面是一个简单的配置示例文件:

package 'example'

config 'example' 'test'
        option   'string'      'some value'
        option   'boolean'     '1'
        list     'collection'  'first item'
        list     'collection'  'second item'

config 'example' 'test' 语句标志着一个节的开始。这里的配置类型是example,配置名是test。配置中也允许出现匿名节,即自定义了配置类型,而没有配置名的节。配置类型对应配置处理程序来说是十分重要的,因为配置程序需要根据这些信息来处理这些配置项。
option 'string' 'some value' 和 option 'boolean' '1' 定义了一些简单值。文本选项和布尔选项在语法上并没有差异。布尔选项中可以用'0' , 'no', 'off', 或者'false'来表示false值,或者也可以用'1', 'yes','on'或者'true'来表示真值。
以list关键字开头的多个行,可用于定义包含多个值的选项。所有共享一个名称的list语句,会组装形成一个值列表,列表中每个值出现的顺序,和它在配置文件中的顺序相同。如上例种中,列表的名称是'collection',它包含了两个值,即'first item'和'second item'。
'option'和'list'语句的缩进可以增加配置文件的可读性,但是在语法不是必须的。
通常不需要为标识符和值加引号,只有当值包括空格或者制表符的时候,才必须加引号。同时,在使用引号的时候,可以用双引号代替单引号。

下面列举的例子都是符合uci语法的正确配置:

option example value
option 'example' value
option example "value"
option "example"    'value' 
option   'example' "value"

反之,以下配置则存在语法错误

option 'example" "value' (引号不匹配)
option example some value with space (值中包含空格,需要为值加引号)
还有一点是必须知道的,即UCI标识符和配置文件名称所包含的字符必须是由a-z, 0-9和_组成。 选项值则可以包含任意字符,只要这个值是加了引号的。

命令行实用工具

使用awk、grep等命令来解析Openwr的配置文件是低效和不明智的做法,建议用户通过uci工具对openwrt进行配置。

下面将给出一些例子,来展示uci这个强大的工具。

用法
root@OpenWrt:/lib/config# uci

用法: uci [] []

命令:

batch
export     [<config>]
import     [<config>]
changes    [<config>]
commit     [<config>]
add        <config> <section-type>
add_list   <config>.<section>.<option>=<string>
show       [<config>[.<section>[.<option>]]]
get        <config>.<section>[.<option>]
set        <config>.<section>[.<option>]=<value>
delete     <config>[.<section[.<option>]]
rename     <config>.<section>[.<option>]=<name>
revert     <config>[.<section>[.<option>]]

参数:

-c <path>  set the search path for config files (default: /etc/config)
-d <str>   set the delimiter for list values in uci show
-f <file>  use <file> as input instead of stdin
-m         when importing, merge data into an existing package
-n         name unnamed sections on export (default)
-N         don't name unnamed sections
-p <path>  add a search path for config change files
-P <path>  add a search path for config change files and use as default
-q         quiet mode (don't print error messages)
-s         force strict mode (stop on parser errors, default)
-S         disable strict mode
-X         do not use extended syntax on 'show'

例子:

导出整个配置

root@OpenWrt:~# uci export uhttpd
package 'uhttpd'

config 'uhttpd' 'main'
    list 'listen_http' '0.0.0.0:80'
    list 'listen_http' '0.0.0.0:8080'
    list 'listen_https' '0.0.0.0:443'
    option 'home' '/www'
    option 'rfc1918_filter' '1'
    option 'cert' '/etc/uhttpd.crt'
    option 'key' '/etc/uhttpd.key'
    option 'cgi_prefix' '/cgi-bin'
    option 'script_timeout' '60'
    option 'network_timeout' '30'
    option 'tcp_keepalive' '1'

config 'cert' 'px5g'
    option 'days' '730'
    option 'bits' '1024'
    option 'country' 'DE'
    option 'state' 'Berlin'
    option 'location' 'Berlin'
    option 'commonname' 'OpenWrt'

root@OpenWrt:~#

查看所有配置项的值

root@OpenWrt:~# uci show uhttpd
uhttpd.main=uhttpd
uhttpd.main.listen_http=0.0.0.0:80 0.0.0.0:8080
uhttpd.main.listen_https=0.0.0.0:443
uhttpd.main.home=/www
uhttpd.main.rfc1918_filter=1
uhttpd.main.cert=/etc/uhttpd.crt
uhttpd.main.key=/etc/uhttpd.key
uhttpd.main.cgi_prefix=/cgi-bin
uhttpd.main.script_timeout=60
uhttpd.main.network_timeout=30
uhttpd.main.tcp_keepalive=1
uhttpd.px5g=cert
uhttpd.px5g.days=730
uhttpd.px5g.bits=1024
uhttpd.px5g.country=DE
uhttpd.px5g.state=Berlin
uhttpd.px5g.location=Berlin
uhttpd.px5g.commonname=OpenWrt
root@OpenWrt:~#

查看特定配置项的值

root@OpenWrt:~# uci get uhttpd.@uhttpd[0].listen_http
0.0.0.0:80 0.0.0.0:8080
root@OpenWrt:~#

查询网络接口的状态

root@OpenWrt:~# uci -P/var/state show network.wan
network.wan=interface
network.wan.ifname=eth0.1
network.wan.proto=dhcp
network.wan.defaultroute=0
network.wan.peerdns=0
network.wan.device=eth0.1
network.wan.ipaddr=10.11.12.13
network.wan.broadcast=255.255.255.255
network.wan.netmask=255.255.255.0
network.wan.gateway=10.11.12.1
network.wan.dnsdomain=
network.wan.dns=10.11.12.100 10.11.12.200
network.wan.up=1
network.wan.lease_gateway=10.11.12.1
network.wan.lease_server=10.11.12.25
network.wan.lease_acquired=1262482940
network.wan.lease_lifetime=5400
network.wan.lease_hostname=x-10-11-12-13
root@OpenWrt:~#

添加防火墙规则

这是一个添加SSH端口转发到防火墙规则的例子,和'-1'使用的一个例子。

root@OpenWrt:~# uci add firewall rule
root@OpenWrt:~# uci set firewall.@rule[-1].src=wan
root@OpenWrt:~# uci set firewall.@rule[-1].target=ACCEPT
root@OpenWrt:~# uci set firewall.@rule[-1].proto=tcp
root@OpenWrt:~# uci set firewall.@rule[-1].dest_port=22
root@OpenWrt:~# uci commit firewall
root@OpenWrt:~# /etc/init.d/firewall restart

本文章由http://www.wifidog.pro/2015/07/27/openwrt-uci%E8%AF%AD%E6%B3%95.html整理编辑,转载请注明出处

标签: openwrt wifidog, openwrt, openwrt 开发, openwrt uci