openwrt高级配置(汗 照着标题就翻译过来了)less
openwrt Kamikaze 8.09的通常配置文件都在目录 /etc/config 下面,可使用脚原本调用参数和设置参数。 好比 sbin/wifi(函数库在 madwifi.sh 脚本)里面就是这样配置本机上的无线网卡的。ide
通常来讲,每一个configure文件都是由一些 section 组成的,section 里面包含了option ,option 都会有一个值。 section定义包含了type和name ,其中 name不是必须定义的,若没有定义,系统会按照cfg**的形式分配给该section一个name。函数
举例来讲,wifi的配置文件 /etc/config/wireless 内容以下:this
******************************file: /etc/config/wireless ******************************************翻译
config wifi-device wifi0
option type atheros
option channel auto
option hwmode 11g
option disabled 0ip
config wifi-iface
option device wifi0
option network lan
option mode adhoc
option ssid OpenWrt
option encryption noneget
*****************************************************************************************************io
这个配置文件里面有两个section, 其type分别是wifi-device 和wifi-iface。其中wifi-device拥有一个name是 wifi0 而wifi-iface没有。ast
能够看到,option 具体设置了驱动或者应用程序的配置,每一项option都有name和value。function
##################################华丽的分割线#######################
利用脚本能够配置这些configure file 。
首先,脚本须要添加. /etc/functions.sh库。而后,使用命令config_load <name> 来加载相应的配置文件,<name>表明配置文件的文件名,函数会在 /etc/config 目录下寻找名字为 name 的文件。
配置文件加载后,可使用函数 config_cb() option_cb() config_foreach() config_get() 以及命令config_foreach 。详细使用以下:
1)config_cb()和option_cb() 函数是指每个section或option执行的callback函数。就是说,config_load 命令加载的config文件中 , 每个section都会调用config_cb()一次,每个option都会调用option_cb()一次。其中,最后一个section的name能够用CONFIG_SECTION引用到。在全部section被调用完以后,config_cb()还会被调用一次,须要注意的是,config_cb()和option_cb() 函数须要在config_load 命令前定义(在. /etc/functions.sh被添加后)。
代码以下:
config_cb() {
local type="$1"
local name="$2"
# commands to be run for every section
}
注:变量$1 和$2分别表明section的type和name
option_cb() {
# commands to be run for every option
}
注:变量$1 和$2分别表明option的name和value
2)config_foreach命令在config_load后使用,方法以下:
config_foreach <function name> [<sectiontype>] [<arguments...>]
这条命令表示对每个section都执行函数function name,该函数在其余地方定义。函数的参数 $1表示该section的name。若是添加sectiontype ,则只有该type的section才能够执行function name 函数。
3)config_get 命令,很简单,不做解释。
# print the value of the option
config_get <section> <option>
# store the value inside the variable
config_get <variable> <section> <option>
注:section栏填写的是section的name,注意文件中没有设定的name是由系统分配的,此种状况最好仍是用config_cb()或者option_cb()函数来完成功能比较好。
4)config_set 命令:
config_set <section> <option> <value>
这个 也很简单不作解释 。只是貌似不能更改实际文件中的option的value而已,仅仅是在当前load的“文件下”。
脚本实例:
file: config_custom.sh
#this scripts is for advanced configure test , by huasion
. /etc/functions.sh
config_cb(){
local type="$1"
local name="$2"
if [ "$1" != "" ] && [ "$2" != "" ]; then
echo "my type is $1, my name is $2"
fi
#echo "last my type is $1, my name is $2"
}
option_cb(){
echo "option is $1 and option name is $2"
}
test(){
echo "fisrt varible is $1"
}
config_load wireless
echo "now do the last job"
config_get foo wifi0 type
echo "$foo"
echo "$CONFIG_SECTION"
config_get wifi0 channel
config_set wifi0 hwmode 11b
config_foreach test wifi-iface
#file is over
file: /etc/config/wireless
config wifi-device wifi0
option type atheros
option channel auto
option hwmode 11g
# REMOVE THIS LINE TO ENABLE WIFI:
#option disabled 1
config wifi-iface
option device wifi0
option network lan
option mode adhoc
option ssid OpenWrt
option encryption none
config wifi-device wifi1
option type atheros
option channel auto
# REMOVE THIS LINE TO ENABLE WIFI:
#option disabled 1
config wifi-iface
option device wifi1
option network lan
option mode adhoc
option ssid OpenWrt
option encryption none
#file is over
执行结果以下:
my type is wifi-device, my name is wifi0
option is type and option name is atheros
option is channel and option name is auto
option is hwmode and option name is 11g
my type is wifi-iface, my name is cfg03c84e
option is device and option name is wifi0
option is network and option name is lan
option is mode and option name is adhoc
option is ssid and option name is OpenWrt
option is encryption and option name is none
my type is wifi-device, my name is wifi1
option is type and option name is atheros
option is channel and option name is auto
my type is wifi-iface, my name is cfg0695cf
option is device and option name is wifi1
option is network and option name is lan
option is mode and option name is adhoc
option is ssid and option name is OpenWrt
option is encryption and option name is none
now do the last job
atheros
cfg0695cf
auto
option is hwmode and option name is 11b
fisrt varible is cfg03c84e
fisrt varible is cfg0695cf
打不动字了 分析以上结果就能够清晰地看到每一个函数在何时被调用,参数状况和函数都作了些什么事,完了,撒花。