[openwrt] uci 的shell和lua接口

uci是openwrt上配置操作的接口,不管是自动化的shell脚本,还是使用luci来二次开发配置界面,都会用到这部分知识。 uci提供了lua, shell, c接口,这里主要用到了前两种

shell接口

文档地址 ,增删改查都有,这里简单使用下。

下面的配置为例子

root@xCloud:~# cat /etc/config/test

config test 'abc'
option test_var2 'value22'
option test_var 'value11'

config tt1
option name 'lzz'

查看配置

root@xCloud:~# uci show test
test.abc=test
test.abc.test_var2='value22'
test.abc.test_var='value11'
test.@tt1[0]=tt1
test.@tt1[0].name='lzz'

显示配置的文本

root@xCloud:~# uci export test
package test

config test 'abc'
option test_var2 'value22'
option test_var 'good'

config tt1
option name 'lzz'

修改一个配置项的值

oot@xCloud:~# uci set test.abc.test_var="good"
root@xCloud:~# uci commit test
root@xCloud:~# uci show test.abc.test_var
test.abc.test_var='good'

获取一个配置项的值

root@xCloud:~# uci get test.abc.test_var
good
root@xCloud:~# uci show test.abc.test_var
test.abc.test_var='good'
root@xCloud:~# uci show test.@tt1[0].name
test.cfg03af7e.name='lzz'
root@xCloud:~# uci get test.@tt1[0].name
lzz

lua接口

通过下面的例子理解

#!/usr/bin/lua
print("Testing start..")
require("uci")

-- Get asection type or an option
x =uci.cursor()
a =x:get("test", "abc", "test_var")

if a == nil then
print("can't found the config file")
return
else
print(a)
end

x:set("test", "abc", "test_var", "value11")
tt1 = x:add("test", "tt1")
x:set("test", tt1, "name", "lzz")

-- Getthe configuration directory
b =x:get_confdir()
print(b)

-- Getall sections of a config or all values of a section
d = x:get_all("test", "abc")
print("config test abc")
print(d)
print(d["test_var"])
print(d["test_var2"])

-- loop a config
x:foreach("test", "tt1", function(s)
    print("----")
    for k,v in pairs(s) do
print(k..":"..tostring(v))
    end
end)

-- discard changes, that have not been commited
-- x:revert("test")

-- commit changes
x:commit("test")

--[[
/etc/config/test
config 'test' 'abc'
    option 'test_var' 'value'
    option 'test_var2' 'value22'
]]

代码最后的注释是测试config的路径和内容

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenWRT是一个基于Linux的嵌入式操作系统,它提供了一套名为UCI(Unified Configuration Interface)的配置接口,用于管理系统的配置文件。UCI提供了C语言接口,使开发者可以通过编程方式读取和修改系统配置。 在使用OpenWRTUCI C语言接口之前,需要包含相应的头文件和链接相关的库文件。头文件是`uci.h`,库文件是`libuci.so`。 下面是一个简单的例子,演示如何使用UCI C语言接口读取和修改配置: ```c #include <stdio.h> #include <uci.h> int main() { struct uci_context *ctx = uci_alloc_context(); if (!ctx) { fprintf(stderr, "Failed to allocate UCI context\n"); return 1; } struct uci_package *pkg; if (uci_load(ctx, "wireless", &pkg) != UCI_OK) { fprintf(stderr, "Failed to load wireless package\n"); uci_free_context(ctx); return 1; } struct uci_element *elem; uci_foreach_element(&pkg->sections, elem) { struct uci_section *section = uci_to_section(elem); const char *name = section->e.name; const char *option_value = uci_lookup_option_string(ctx, section, "option_name"); printf("Section: %s\n", name); printf("Option value: %s\n", option_value); } uci_unload(ctx, pkg); uci_free_context(ctx); return 0; } ``` 上述代码中,首先通过`uci_alloc_context()`函数分配一个UCI上下文对象。然后使用`uci_load()`函数加载指定的配置包(这里是"wireless")。接着使用`uci_foreach_element()`函数遍历配置包中的所有节(section),并通过`uci_lookup_option_string()`函数获取指定选项(option)的值。最后,使用`uci_unload()`函数卸载配置包,并通过`uci_free_context()`函数释放UCI上下文对象。 以上是一个简单的示例,你可以根据具体的需求进一步扩展和修改代码。希望对你有帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值