UCI shell API使用说明
uci shell接口定义在/lib/functions.sh文件中,参考openwrt官网[OpenWrt Wiki] Configuration in scripts。
1.config_cb()、option_cb()和list_cd()函数
UCI每次解析section时都会调用callback;config_load完成后会生成对config_cb、option_cb和list_cb()的调用。如果在解析每个选项时需要保存一些数据进行操作,需要定义三个config_cb()、option_cb()和list_cb()函数。
使用举例:
●测试脚本
cat test.sh
#! /bin/sh
. /lib/functions.sh
config_cb() {
local type="$1"
local name="$2"
echo "config_cb type= $type"
echo "config_cb name= $name"
# commands to be run for every section
}
option_cb() {
local name="$1"
local value="$2"
echo "option_cb name=: $name"
echo "option_cb value=: $value"
# commands to be run for every option
}
list_cb() {
local name="$1"
local value="$2"
echo "list_cb name=: $name"
echo "list_cb value=: $value"
# commands to be run for every list item
}
config_load network
●network脚本:
cat /etc/config/network
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config globals 'globals'
option ula_prefix 'auto'
config switch
option name 'switch0'
config interface 'lan'
option ifname 'eth0 eth1'
option force_link '1'
option type 'bridge'
option proto 'static'
option ipaddr '192.168.3.1'
option netmask '255.255.255.0'
option ip6assign '60'
option multicast_querier '0'
option igmp_snooping '0'
option ieee1905managed '1'
config interface 'wan'
option ifname 'eth2'
option proto 'dhcp'
config interface 'wan6'
option ifname 'eth2'
option proto 'dhcpv6'
●测试结果:
./test.sh
config_cb type= interface
config_cb name= loopback
option_cb name=: ifname
option_cb value=: lo
option_cb name=: proto
option_cb value=: static
option_cb name=: ipaddr
option_cb value=: 127.0.0.1
option_cb name=: netmask
option_cb value=: 255.0.0.0
option_cb name=: up
option_cb value=: 1
option_cb name=: device
option_cb value=: lo
config_cb type= globals
config_cb name= globals
option_cb name=: ula_prefix
option_cb value=: auto
config_cb type= switch
config_cb name= cfg033777
option_cb name=: name
option_cb value=: switch0
config_cb type= interface
config_cb name= lan
option_cb name=: force_link
option_cb value=: 1
option_cb name=: type
option_cb value=: bridge
option_cb name=: proto
option_cb value=: static
option_cb name=: ipaddr
option_cb value=: 192.168.3.1
option_cb name=: netmask
option_cb value=: 255.255.255.0
option_cb name=: ip6assign
option_cb value=: 60
option_cb name=: multicast_querier
option_cb value=: 0
option_cb name=: igmp_snooping
option_cb value=: 0
option_cb name=: ieee1905managed
option_cb value=: 1
option_cb name=: up
option_cb value=: 1
option_cb name=: device
option_cb value=: eth0 eth1
option_cb name=: ifname
option_cb value=: br-lan
config_cb type= interface
config_cb name= wan
option_cb name=: ifname
option_cb value=: eth2
option_cb name=: proto
option_cb value=: dhcp
option_cb name=: up
option_cb value=: 1
option_cb name=: device
option_cb value=: eth2
config_cb type= interface
config_cb name= wan6
option_cb name=: ifname
option_cb value=: eth2
option_cb name=: proto
option_cb value=: dhcpv6
config_cb type=
config_cb name=
2.config_load函数:
在执行uci的shell接口的时候,都需要先执行config_load函数将package加载进环境中。
函数格式:config_load <package>
脚本源码:
config_load() {
[ -n "$IPKG_INSTROOT" ] && return 0
uci_load "$@"
}
3.config_foreach函数:
config_foreach基于回调解析的一种方法过程的迭代函数。
格式:config_foreach <callback_func> [<section type>] ]<customer_val>]
callback_func回调函数,section_type配置文件类型,若不设置该参数,则获取脚本所有的类型,customer_val变量可选,将脚本类型赋值到customer_val变量。
脚本源码:
config_foreach() {
local ___function="$1"
[ "$#" -ge 1 ] && shift
local ___type="$1"
[ "$#" -ge 1 ] && shift
local section cfgtype
[ -z "$CONFIG_SECTIONS" ] && return 0
for section in ${CONFIG_SECTIONS}; do
config_get cfgtype "$section" TYPE
[ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue
eval "$___function \"\$section\" \"\$@\""
done
}
应用举例:
●脚本内容
cat /etc/config/network
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config globals 'globals'
option ula_prefix 'auto'
config switch
option name 'switch0'
config interface 'lan'
option ifname 'eth0 eth1'
option force_link '1'
option type 'bridge'
option proto 'static'
option ipaddr '192.168.3.1'
option netmask '255.255.255.0'
option ip6assign '60'
option multicast_querier '0'
option igmp_snooping '0'
option ieee1905managed '1'
config interface 'wan'
option ifname 'eth2'
option proto 'dhcp'
config interface 'wan6'
option ifname 'eth2'
option proto 'dhcpv6'
●测试脚本:
#! /bin/sh
. /lib/functions.sh
handle_interface() {
local config="$1"
local custom="$2"
echo "$custom $config"
# run commands for every interface section
}
config_load network
config_foreach handle_interface interface interfacetype:
●执行结果:
interfacetype: loopback
interfacetype: lan
interfacetype: wan
interfacetype: wan6、
4.config_get函数:
格式:config_get <variable> <setction name> <option name> [<default>]
config_get从etction name中获取option name的vlaue保存到variable中,若option name的vlaue存在就返回value,若不存在就返回default。
应用举例1(以/etc/config/network为例说明):
●测试脚本1
#! /bin/sh
. /lib/functions.sh
config_load network
config_get ifacex 'loopback' ipaddr
echo "------- is $ifacex"
●执行结果:------- is 127.0.0.1
●测试脚本2
config_load network
config_get ifacex 'lan' type
echo "------- is $ifacex"
●执行结果------- is bridge
●测试脚本3
config_load wireless
config_get ifacex 'wifi1' type
echo "------- is $ifacex"
●执行结果:------- is qcawificfg80211
应用举例2(以/etc/config/system为例说明)
●测试脚本4
配置文件内容
cat /etc/config/system
config system
option hostname OpenWrt
option timezone UTC
option log_size 64
config timeserver ntp
list server 0.openwrt.pool.ntp.org
list server 1.openwrt.pool.ntp.org
list server 2.openwrt.pool.ntp.org
list server 3.openwrt.pool.ntp.org
option enabled 1
option enable_server 0
脚本:
#! /bin/sh
. /lib/functions.sh
config_load system
config_get ifacex 'ntp' server
echo "------- is $ifacex"
结果:------- is 0.openwrt.pool.ntp.org 1.openwrt.pool.ntp.org 2.openwrt.pool.ntp.org 3.openwrt.pool.ntp.org
5.config_set函数:
config_set将value设置到对象中。
格式:config_set <section> <option> <value>
config_set更改的值仅保存在内存中。对config_get的后续调用将返回更新后的值,底层配置文件不会更改。如果要更改值,需要使用/etc/functions.sh自动包含的/lib/config/uci.sh中的uci_*函数。
6.config_list_foreach函数:
config_list_foreach与config_foreach类似都是以回调解析的一种方法过程迭代,主要用于list的section选项。
函数格式:config_list_foreach <section name> <list name> <handle_back>
应用举例(以/etc/config/system例说明):
●配置文件
cat /etc/config/system
config system
option hostname OpenWrt
option timezone UTC
option log_size 64
config timeserver ntp
list server 0.openwrt.pool.ntp.org
list server 1.openwrt.pool.ntp.org
list server 2.openwrt.pool.ntp.org
list server 3.openwrt.pool.ntp.org
option enabled 1
option enable_server 0
●测试脚本:
#! /bin/sh
. /lib/functions.sh
handle_ntp() {
local value="$1"
echo "$value"
# do something with $value
}
config_load system
config_list_foreach ntp server handle_ntp
●结果
0.openwrt.pool.ntp.org
1.openwrt.pool.ntp.org
2.openwrt.pool.ntp.org
3.openwrt.pool.ntp.org
7.config_get_bool函数:
与config_get功能类似。将返回值转换成bool类型
8.append函数:
格式:append <variable2> <variable1>
variable1的变量是从config_get获取的数据,append就是将variable1的数据保存到variable2变量中
●测试脚本:
从/etc/config/network中保存wifi1的channel、hwmode、htmode的值保存到axx.conf文件
#! /bin/sh
. /lib/functions.sh
config_load wireless
config_get ch 'wifi1' channel
echo "1.-----$ch"
append _buffer "channel $ch"
config_get hwmd 'wifi1' hwmode
echo "2.----$hwmd"
append _buffer "hwmode $hwmd"
config_get htmd 'wifi1' htmode
echo "3.----$htmd"
append _buffer "htmode $htmd"
echo "$_buffer" >> /tmp/axx.conf
●结果:
cat axx.conf
channel auto hwmode 11axg htmode HT40
9.list_contains:
格式:list_contains <variable1> <variable2>
list_contains从左到右将变量variable2的内容从变量variable1的尾部删除。
●脚本源码:
list_contains() {
local var="$1"
local str="$2"
local val
eval "val=\" \${$var} \""
[ "${val%% $str *}" != "$val" ]
}
●应用举例:
#! /bin/sh
test=ssssyyyyxxxxyyyy111
echo "${test%%yy*}"
结果:ssss