root@OpenWrt:/# ubus call system board
{
"kernel": "3.18.8",
"hostname": "OpenWrt",
"system": "Atheros AR9341 rev 1",
"model": "Atheros DB120 reference board",
"release": {
"distribution": "OpenWrt",
"version": "Bleeding Edge",
"revision": "r44627",
"codename": "chaos_calmer",
"target": "ar71xx\/generic",
"description": "OpenWrt Chaos Calmer r44627"
}
}
root@OpenWrt:/#
root@OpenWrt:/# cat /test.sh
#!/bin/sh
# Copyright (C) 2011 OpenWrt.org
. /usr/share/libubox/jshn.sh
json_load "$(ubus call system board)"
json_get_var model model
echo $model
root@OpenWrt:/# sh test.sh
model=Atheros DB120 reference board
root@OpenWrt:/#
通过ubus call network.interface.wan status 读取路由参数,及target参数
1)数据如下:
root@tz:/etc/hotplug.d# ubus call network.interface.wan status
{
"up": true,
"pending": false,
"available": true,
"autostart": true,
"uptime": 5472,
"l3_device": "eth0.2",
"proto": "dhcp",
"device": "eth0.2",
"metric": 0,
"delegation": true,
"ipv4-address": [
{
"address": "192.168.66.133",
"mask": 24
}
],
"ipv6-address": [
],
"ipv6-prefix": [
],
"ipv6-prefix-assignment": [
],
"route": [
{
"target": "192.168.66.1",
"mask": 32,
"nexthop": "0.0.0.0",
"source": "192.168.66.133\/32"
},
{
"target": "0.0.0.0",
"mask": 0,
"nexthop": "192.168.66.1",
"source": "192.168.66.133\/32"
}
],
"dns-server": [
"114.114.114.114"
],
"dns-search": [
"lan"
],
"inactive": {
"ipv4-address": [
],
"ipv6-address": [
],
"route": [
],
"dns-server": [
],
"dns-search": [
]
},
"data": {
"leasetime": 120
}
}
root@tz:/etc/hotplug.d#
2)脚本如下:
root@tz:/etc/hotplug.d# cat /tesh.sh
#!/bin/sh
. /usr/share/libubox/jshn.sh
ubus_call() {
json_init
local _data="$(ubus -S call "$1" "$2")"
[ -z "$_data" ] && return 1
json_load "$_data"
return 0
}
start()
{
ubus_call "network.interface.wan" status || return 0
json_get_var up up
echo "up=$up"
json_select route
json_get_keys route
echo "keys=$route"
json_select 1
json_get_vars target target
echo "target=$target"
}
start
root@tz:/etc/hotplug.d#
3)测试结果:
root@tz:/etc/hotplug.d# sh /tesh.sh
up=1
keys= 1 2
target=192.168.66.1
root@tz:/etc/hotplug.d#
4)简单总结:
json_select $1:如果有2级json数据,可以用此命令进行读取
json_get_vars:获取2级json数据的个数,如router有2个;
"route": [
{
"target": "192.168.66.1",
"mask": 32,
"nexthop": "0.0.0.0",
"source": "192.168.66.133\/32"
},
{
"target": "0.0.0.0",
"mask": 0,
"nexthop": "192.168.66.1",
"source": "192.168.66.133\/32"
}
],