OpenWRT ubus

ubus


ubus

git clone http://git.openwrt.org/project/ubus.git

source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 列出所有已註冊 server
ubus list
# 列出所有已註冊 server 介面
ubus list -v
# 列出 uci 介面
ubus list -v uci
'uci' @8f32228e
"get":{"config":"String","section":"String","option":"String","type":"String","match":"Table","ubus_rpc_session":"String"}
# ...

uci get network.wan.proto
dhcp

ubus call uci get '{"config":"network","section":"wan","option":"proto"}'
{
"value": "dhcp"
}

uhttpd-mod-ubus

預設可以透過 /ubus post 並用 jsonrpc v2.0 溝通。

acl group : /usr/share/rpcd/acl.d/*.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
curl -d '
{
"jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": [
"00000000000000000000000000000000",
"session",
"login",
{
"username": "root",
"password": "80743314"
}
]
}' http://your.server.ip/ubus

{
"jsonrpc": "2.0",
"id": 1,
"result": [
0,
{
"ubus_rpc_session": "4281cba1179fb1aee97f88bebe07e3f5",
"timeout": 300,
"expires": 299,
"acls": {
"access-group": {
"linkit": [
"read",
"write"
],
"unauthenticated": [
"read"
],
"yume": [
"read",
"write"
]
},
"cgi-io": {
"backup": [
"read"
],
"upload": [
"write"
]
},
"ubus": {
"*": [
"*"
],
"iwinfo": [
"scan",
"info"
],
"network.interface": [
"status"
],
"rpc-sys": [
"*"
],
"session": [
"grant",
"destroy",
"logout",
"access",
"login"
],
"system": [
"board"
],
"uci": [
"get",
"set",
"apply",
"reload_config"
]
},
"uci": {
"*": [
"read",
"write"
],
"network": [
"read",
"write"
],
"system": [
"read",
"write"
],
"wireless": [
"read",
"write"
]
}
},
"data": {
"username": "root"
}
}
]
}

ubus list -v uci
'uci' @dd090f7f
"get":{"config":"String","section":"String","option":"String","type":"String","match":"Table","ubus_rpc_session":"String"}
# ...

uci get network.wan.proto
dhcp

curl -d '
{
"jsonrpc": "2.0",
"id": "1",
"method": "call",
"params": [
"20df7c4a82e9a28a63a5908c6fd2a4af",
"uci",
"get",
{
"config": "network",
"section": "wan",
"option": "proto"
}
]
}‘ http://your.server.ip/ubus
{
"jsonrpc": "2.0",
"id": "1",
"result": [
0,
{
"value": "dhcp"
}
]
}

lua ubus

1
2
3
4
5
ubus = require 'ubus'
conn = ubus.connect()
s = conn:call("uci",'get',{config="network",section="wan",option="proto"})
for k,v in pairs(s) do print(k.." "..tostring(v)) end
-- value dhcp