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. ... 
  5. } 

/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. { 
  3. "sid": "f9ebb2dc7add7dc446165cd530c1b217", 
  4. "timeout": 280000, 
  5. "touched": 1790, 
  6. "acls": { 
  7.  
  8. }, 
  9. "data": { 
  10.  
  11. } 
  12. } 

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

  1. root@llwang-test:~# ubus call session list 
  2. { 
  3. "sid": "f9ebb2dc7add7dc446165cd530c1b217", 
  4. "timeout": 280000, 
  5. "touched": 1790, 
  6. "acls": { 
  7.  
  8. }, 
  9. "data": { 
  10.  
  11. } 
  12. } 
  13.  
  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. { 
  3. "sid": "f9ebb2dc7add7dc446165cd530c1b217", 
  4. "timeout": 280000, 
  5. "touched": 2598, 
  6. "acls": { 
  7. "network.interface.loopback": [ 
  8. "status" 
  9. ] 
  10. }, 
  11. "data": { 
  12.  
  13. } 
  14. } 
  15.  
  1. 这时远程PC主机可以通过HTTP请求来访问OpenWRT
  1. llwang@ubuntuVBox~ $ curl http://172.16.0.171/ubus/f9ebb2dc7add7dc446165cd530c1b217/network.interface.loopback/status 
  2. { 
  3. "up": true, 
  4. "pending": false, 
  5. "available": true, 
  6. "autostart": true, 
  7. "uptime": 2880, 
  8. "l3_device": "lo", 
  9. "proto": "static", 
  10. "device": "lo", 
  11. "metric": 0, 
  12. "ipv4-address": [ 
  13. { 
  14. "address": "127.0.0.1", 
  15. "mask": 8 
  16. } 
  17. ], 
  18. "ipv6-address": [ 
  19.  
  20. ], 
  21. "ipv6-prefix": [ 
  22.  
  23. ], 
  24. "ipv6-prefix-assignment": [ 
  25.  
  26. ], 
  27. "route": [ 
  28.  
  29. ], 
  30. "dns-server": [ 
  31.  
  32. ], 
  33. "dns-search": [ 
  34.  
  35. ], 
  36. "inactive": { 
  37. "ipv4-address": [ 
  38.  
  39. ], 
  40. "ipv6-address": [ 
  41.  
  42. ], 
  43. "route": [ 
  44.  
  45. ], 
  46. "dns-server": [ 
  47.  
  48. ], 
  49. "dns-search": [ 
  50.  
  51. ] 
  52. }, 
  53. "data": { 
  54.  
  55. } 
  56. } 
  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. { 
  3. "sid": "f9ebb2dc7add7dc446165cd530c1b217", 
  4. "timeout": 280000, 
  5. "touched": 4799, 
  6. "acls": { 
  7. "network.device": [ 
  8. "status" 
  9. ], 
  10. "network.interface.loopback": [ 
  11. "status" 
  12. ], 
  13. "uci": [ 
  14. "configs", 
  15. "get" 
  16. ] 
  17. }, 
  18. "data": { 
  19.  
  20. } 
  21. } 
  22.  
  1. 远程PC通过POST主机携带参数访问
  1. llwang@ubuntuVBox~ $ curl -d '{"name":"eth0"}' http://172.16.0.171/ubus/f9ebb2dc7add7dc446165cd530c1b217/network.device/status 
  2. { 
  3. "external": false, 
  4. "present": true, 
  5. "type": "Network device", 
  6. "up": true, 
  7. "link": true, 
  8. "link-advertising": [ 
  9. "1000F" 
  10. ], 
  11. "link-supported": [ 
  12. "1000F" 
  13. ], 
  14. "speed": "1000F", 
  15. "mtu": 1500, 
  16. "macaddr": "fc:ad:0f:07:ed:50", 
  17. "txqueuelen": 1000, 
  18. "statistics": { 
  19. "collisions": 0, 
  20. "rx_frame_errors": 0, 
  21. "tx_compressed": 0, 
  22. "multicast": 0, 
  23. "rx_length_errors": 0, 
  24. "tx_dropped": 0, 
  25. "rx_bytes": 8890214, 
  26. "rx_missed_errors": 0, 
  27. "tx_errors": 0, 
  28. "rx_compressed": 0, 
  29. "rx_over_errors": 0, 
  30. "tx_fifo_errors": 0, 
  31. "rx_crc_errors": 0, 
  32. "rx_packets": 14620, 
  33. "tx_heartbeat_errors": 0, 
  34. "rx_dropped": 0, 
  35. "tx_aborted_errors": 0, 
  36. "tx_packets": 16941, 
  37. "rx_errors": 0, 
  38. "tx_bytes": 2138781, 
  39. "tx_window_errors": 0, 
  40. "rx_fifo_errors": 0, 
  41. "tx_carrier_errors": 0 
  42. } 
  43. } 

参考资料

  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  阅读(1948)  评论(0)    收藏  举报

导航