how to use ubus over http in openwrt 12.09
how to use ubus over http in openwrt 12.09
背景
开发使用的Openwrt版本是12.09,其中uhttpd版本2012-10-30,支持uhttpd-mod-ubus, 但是openwrt wiki上表述的是比较新的版本,使用上不太一样,就花些时间研究了下code,把ubus over http在12.09上运行起来。
使用方法
- 修改/etc/init.d/uhttpd /etc/config/uhttp添加 uhttpd -u ubus_prefix支持
/etc/init.d/uhttpd
- start_instance() {
- ...
- append_arg "$cfg" ubus_prefix "-u"
- ...
- }
/etc/config/uhttpd
- ...
- option ubus_prefix /ubus
- ...
- 通过/etc/init.d/uhttpd restart,重启uhttpd。应该能看到如下uhttpd参数
- root@llwang-test:~# ps w | grep uhttpd
- 1859 root 1584 S /usr/sbin/uhttpd -f -h /www -r llwang-test -x /cgi-bin -u /ubus -t 120 -T 30 -A 1 -n 3 -R -p 0.0.0.0:80
- 2459 root 1536 S grep uhttpd
- 在OpenWRT上创建session,指定timeout时间
- root@llwang-test:~# ubus call session create '{ "timeout": 280000 }'
- {
- "sid": "f9ebb2dc7add7dc446165cd530c1b217",
- "timeout": 280000,
- "touched": 1790,
- "acls": {
-
- },
- "data": {
-
- }
- }
通过如下命令能看到这个session
- root@llwang-test:~# ubus call session list
- {
- "sid": "f9ebb2dc7add7dc446165cd530c1b217",
- "timeout": 280000,
- "touched": 1790,
- "acls": {
-
- },
- "data": {
-
- }
- }
-
- 添加对这个session的acls控制,允许这个session访问那些ubus注册的接口,比如添加network操作
- root@llwang-test:~# ubus call session grant '{"sid": "f9ebb2dc7add7dc446165cd530c1b217", "objects":[["network.interface.loopback","status"]] }'
可以通过如下查询是否设置成功
- root@llwang-test:~# ubus call session list
- {
- "sid": "f9ebb2dc7add7dc446165cd530c1b217",
- "timeout": 280000,
- "touched": 2598,
- "acls": {
- "network.interface.loopback": [
- "status"
- ]
- },
- "data": {
-
- }
- }
-
- 这时远程PC主机可以通过HTTP请求来访问OpenWRT
- llwang@ubuntuVBox~ $ curl http://172.16.0.171/ubus/f9ebb2dc7add7dc446165cd530c1b217/network.interface.loopback/status
- {
- "up": true,
- "pending": false,
- "available": true,
- "autostart": true,
- "uptime": 2880,
- "l3_device": "lo",
- "proto": "static",
- "device": "lo",
- "metric": 0,
- "ipv4-address": [
- {
- "address": "127.0.0.1",
- "mask": 8
- }
- ],
- "ipv6-address": [
-
- ],
- "ipv6-prefix": [
-
- ],
- "ipv6-prefix-assignment": [
-
- ],
- "route": [
-
- ],
- "dns-server": [
-
- ],
- "dns-search": [
-
- ],
- "inactive": {
- "ipv4-address": [
-
- ],
- "ipv6-address": [
-
- ],
- "route": [
-
- ],
- "dns-server": [
-
- ],
- "dns-search": [
-
- ]
- },
- "data": {
-
- }
- }
- 添加一个需要参数的接口,比如network.device status ‘{“name”:“device”}’
- root@llwang-test:~# ubus call session grant '{"sid": "f9ebb2dc7add7dc446165cd530c1b217", "objects":[["network.device","status"]] }'
检查是否添加成功
- root@llwang-test:~# ubus call session list
- {
- "sid": "f9ebb2dc7add7dc446165cd530c1b217",
- "timeout": 280000,
- "touched": 4799,
- "acls": {
- "network.device": [
- "status"
- ],
- "network.interface.loopback": [
- "status"
- ],
- "uci": [
- "configs",
- "get"
- ]
- },
- "data": {
-
- }
- }
-
- 远程PC通过POST主机携带参数访问
- llwang@ubuntuVBox~ $ curl -d '{"name":"eth0"}' http://172.16.0.171/ubus/f9ebb2dc7add7dc446165cd530c1b217/network.device/status
- {
- "external": false,
- "present": true,
- "type": "Network device",
- "up": true,
- "link": true,
- "link-advertising": [
- "1000F"
- ],
- "link-supported": [
- "1000F"
- ],
- "speed": "1000F",
- "mtu": 1500,
- "macaddr": "fc:ad:0f:07:ed:50",
- "txqueuelen": 1000,
- "statistics": {
- "collisions": 0,
- "rx_frame_errors": 0,
- "tx_compressed": 0,
- "multicast": 0,
- "rx_length_errors": 0,
- "tx_dropped": 0,
- "rx_bytes": 8890214,
- "rx_missed_errors": 0,
- "tx_errors": 0,
- "rx_compressed": 0,
- "rx_over_errors": 0,
- "tx_fifo_errors": 0,
- "rx_crc_errors": 0,
- "rx_packets": 14620,
- "tx_heartbeat_errors": 0,
- "rx_dropped": 0,
- "tx_aborted_errors": 0,
- "tx_packets": 16941,
- "rx_errors": 0,
- "tx_bytes": 2138781,
- "tx_window_errors": 0,
- "rx_fifo_errors": 0,
- "tx_carrier_errors": 0
- }
- }
参考资料
nicephil@gmail.com 2017-4-26