openwrt advanced configuration

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

打不動字了 分析以上結果就能夠清晰地看到每一個函數在何時被調用,參數狀況和函數都作了些什麼事,完了,撒花。

相關文章
相關標籤/搜索