UBUS 使用例程(五)-json

UBUS 使用例程(五)

1. shell 脚本:ubus 读取参数结合json函数–读取一级数据

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:/# 

2. shell 脚本:ubus 读取参数结合json函数–读取二级数据

通过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"
                }
        ],
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值