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上运行起来。

使用方法

  1. 修改/etc/init.d/uhttpd /etc/config/uhttp添加 uhttpd -u ubus_prefix支持

/etc/init.d/uhttpd

  1. start_instance() { 
  2. ... 
  3. append_arg "$cfg" ubus_prefix "-u" 
  4. ... 

/etc/config/uhttpd

  1. ... 
  2. option ubus_prefix /ubus 
  3. ... 
  1. 通过/etc/init.d/uhttpd restart,重启uhttpd。应该能看到如下uhttpd参数
  1. root@llwang-test:~# ps w | grep uhttpd 
  2. 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 
  3. 2459 root 1536 S grep uhttpd 
  1. 在OpenWRT上创建session,指定timeout时间
  1. root@llwang-test:~# ubus call session create '{ "timeout": 280000 }' 

  2. "sid": "f9ebb2dc7add7dc446165cd530c1b217"
  3. "timeout": 280000
  4. "touched": 1790
  5. "acls": { 
  6.  
  7. }, 
  8. "data": { 
  9.  


通过如下命令能看到这个session

  1. root@llwang-test:~# ubus call session list 

  2. "sid": "f9ebb2dc7add7dc446165cd530c1b217"
  3. "timeout": 280000
  4. "touched": 1790
  5. "acls": { 
  6.  
  7. }, 
  8. "data": { 
  9.  


  10.  
  1. 添加对这个session的acls控制,允许这个session访问那些ubus注册的接口,比如添加network操作
  1. root@llwang-test:~# ubus call session grant '{"sid": "f9ebb2dc7add7dc446165cd530c1b217", "objects":[["network.interface.loopback","status"]] }' 

可以通过如下查询是否设置成功

  1. root@llwang-test:~# ubus call session list 

  2. "sid": "f9ebb2dc7add7dc446165cd530c1b217"
  3. "timeout": 280000
  4. "touched": 2598
  5. "acls": { 
  6. "network.interface.loopback": [ 
  7. "status" 

  8. }, 
  9. "data": { 
  10.  


  11.  
  1. 这时远程PC主机可以通过HTTP请求来访问OpenWRT
  1. llwang@ubuntuVBox~ $ curl http://172.16.0.171/ubus/f9ebb2dc7add7dc446165cd530c1b217/network.interface.loopback/status 

  2. "up": true
  3. "pending": false
  4. "available": true
  5. "autostart": true
  6. "uptime": 2880
  7. "l3_device": "lo"
  8. "proto": "static"
  9. "device": "lo"
  10. "metric": 0
  11. "ipv4-address": [ 

  12. "address": "127.0.0.1"
  13. "mask": 8 

  14. ], 
  15. "ipv6-address": [ 
  16.  
  17. ], 
  18. "ipv6-prefix": [ 
  19.  
  20. ], 
  21. "ipv6-prefix-assignment": [ 
  22.  
  23. ], 
  24. "route": [ 
  25.  
  26. ], 
  27. "dns-server": [ 
  28.  
  29. ], 
  30. "dns-search": [ 
  31.  
  32. ], 
  33. "inactive": { 
  34. "ipv4-address": [ 
  35.  
  36. ], 
  37. "ipv6-address": [ 
  38.  
  39. ], 
  40. "route": [ 
  41.  
  42. ], 
  43. "dns-server": [ 
  44.  
  45. ], 
  46. "dns-search": [ 
  47.  

  48. }, 
  49. "data": { 
  50.  


  1. 添加一个需要参数的接口,比如network.device status ‘{“name”:“device”}’
  1. root@llwang-test:~# ubus call session grant '{"sid": "f9ebb2dc7add7dc446165cd530c1b217", "objects":[["network.device","status"]] }' 

检查是否添加成功

  1. root@llwang-test:~# ubus call session list 

  2. "sid": "f9ebb2dc7add7dc446165cd530c1b217"
  3. "timeout": 280000
  4. "touched": 4799
  5. "acls": { 
  6. "network.device": [ 
  7. "status" 
  8. ], 
  9. "network.interface.loopback": [ 
  10. "status" 
  11. ], 
  12. "uci": [ 
  13. "configs"
  14. "get" 

  15. }, 
  16. "data": { 
  17.  


  18.  
  1. 远程PC通过POST主机携带参数访问
  1. llwang@ubuntuVBox~ $ curl -d '{"name":"eth0"}' http://172.16.0.171/ubus/f9ebb2dc7add7dc446165cd530c1b217/network.device/status 

  2. "external": false
  3. "present": true
  4. "type": "Network device"
  5. "up": true
  6. "link": true
  7. "link-advertising": [ 
  8. "1000F" 
  9. ], 
  10. "link-supported": [ 
  11. "1000F" 
  12. ], 
  13. "speed": "1000F"
  14. "mtu": 1500
  15. "macaddr": "fc:ad:0f:07:ed:50"
  16. "txqueuelen": 1000
  17. "statistics": { 
  18. "collisions": 0
  19. "rx_frame_errors": 0
  20. "tx_compressed": 0
  21. "multicast": 0
  22. "rx_length_errors": 0
  23. "tx_dropped": 0
  24. "rx_bytes": 8890214
  25. "rx_missed_errors": 0
  26. "tx_errors": 0
  27. "rx_compressed": 0
  28. "rx_over_errors": 0
  29. "tx_fifo_errors": 0
  30. "rx_crc_errors": 0
  31. "rx_packets": 14620
  32. "tx_heartbeat_errors": 0
  33. "rx_dropped": 0
  34. "tx_aborted_errors": 0
  35. "tx_packets": 16941
  36. "rx_errors": 0
  37. "tx_bytes": 2138781
  38. "tx_window_errors": 0
  39. "rx_fifo_errors": 0
  40. "tx_carrier_errors": 0 


参考资料

  1. openwrt上关于ubus over http的描述
  2. blob,blobmsg的分析
  3. uhttpd 2012-10-30代码
  4. uhttpd 2012-10-30帮助

nicephil@gmail.com 2017-4-26

posted on 2017-04-26 14:23  nicephil  阅读(1638)  评论(0编辑  收藏  举报

导航