2019/02/12

OpenWRT 取得 wan, lan 的 ip address 和 mask

OpenWRT 的 uci get 是用來取得設定內容,假如 wan 是動態取得 ip address,就無法透過 uci get 來取得 wan 的 ip address。所以需要透過 ubus 跟 jsonfilter 來取得這些動態的資訊。

先參考官方的 ubus 說明

列出現有的 object 來參考
root@OpenWrt:~# ubus list
log
network
network.device
network.interface
network.interface.VLAN101
network.interface.lan
network.interface.loopback
network.interface.wan
network.interface.wan6
network.wireless
service
session
system
uci

看看 wan 的狀態
root@OpenWrt:~# ubus call network.interface.wan status
{
	"up": true,
	"pending": false,
	"available": true,
	"autostart": true,
	"dynamic": false,
	"uptime": 5493,
	"l3_device": "eth0",
	"proto": "dhcp",
	"device": "eth0",
	"updated": [
		"addresses",
		"routes",
		"data"
	],
	"metric": 0,
	"delegation": true,
	"ipv4-address": [
		{
			"address": "192.168.5.218",
			"mask": 24
		}
	],
	"ipv6-address": [
		
	],
	"ipv6-prefix": [
		
	],
	"ipv6-prefix-assignment": [
		
	],
	"route": [
		{
			"target": "192.168.5.254",
			"mask": 32,
			"nexthop": "0.0.0.0",
			"source": "192.168.5.218\/32"
		},
		{
			"target": "0.0.0.0",
			"mask": 0,
			"nexthop": "192.168.5.254",
			"source": "192.168.5.218\/32"
		}
	],
	"dns-server": [
		"8.8.8.8",
		"168.95.1.1"
	],
	"dns-search": [
		"WIFI"
	],
	"inactive": {
		"ipv4-address": [
			
		],
		"ipv6-address": [
			
		],
		"route": [
			
		],
		"dns-server": [
			
		],
		"dns-search": [
			
		]
	},
	"data": {
		"leasetime": 86400
	}
}
輸出是 json 的結構,從上面的內容看到 wan 是透過 dhcp 取得 192.168.5.218/24

接下來要透過 jsonfilter (官網) 來抽出我們想要的內容。不確定原因,但是直接執行沒看到說明,所以進去原始碼看看語法:
print_usage(char *app)
{
	printf(
	"== Usage ==\n\n"
	"  # %s [-a] [-i  | -s \"json...\"] {-t  | -e }\n"
	"  -q		Quiet, no errors are printed\n"
	"  -h, --help	Print this help\n"
	"  -a		Implicitely treat input as array, useful for JSON logs\n"
	"  -i path	Specify a JSON file to parse\n"
	"  -s \"json\"	Specify a JSON string to parse\n"
	"  -l limit	Specify max number of results to show\n"
	"  -F separator	Specify a field separator when using export\n"
	"  -t 	Print the type of values matched by pattern\n"
	"  -e 	Print the values matched by pattern\n"
	"  -e VAR=	Serialize matched value for shell \"eval\"\n\n"
	"== Patterns ==\n\n"
	"  Patterns are JsonPath: http://goessner.net/articles/JsonPath/\n"
	"  This tool implements $, @, [], * and the union operator ','\n"
	"  plus the usual expressions and literals.\n"
	"  It does not support the recursive child search operator '..' or\n"
	"  the '?()' and '()' filter expressions as those would require a\n"
	"  complete JavaScript engine to support them.\n\n"
	"== Examples ==\n\n"
	"  Display the first IPv4 address on lan:\n"
	"  # ifstatus lan | %s -e '@[\"ipv4-address\"][0].address'\n\n"
	"  Extract the release string from the board information:\n"
	"  # ubus call system board | %s -e '@.release.description'\n\n"
	"  Find all interfaces which are up:\n"
	"  # ubus call network.interface dump | \\\n"
	"  	%s -e '@.interface[@.up=true].interface'\n\n"
	"  Export br-lan traffic counters for shell eval:\n"
	"  # devstatus br-lan | %s -e 'RX=@.statistics.rx_bytes' \\\n"
	"	-e 'TX=@.statistics.tx_bytes'\n",
		app, app, app, app, app);
}

我們要用的是 -e 這個操作,以 ip address 為例,要取的值是 ipv4-address 底下的 address
root@OpenWrt:~# ubus call network.interface.wan status | jsonfilter -e '@["ipv4-address"][0].address'
192.168.5.218
很順利的取得了 wan 的 ip 是 192.168.5.218

寫個 shell script 來測試
root@OpenWrt:/# cat /tmp/test.sh 
LAN_IP=`ubus call network.interface.lan status | jsonfilter -e '@["ipv4-address"][0].address'`
LAN_MASK=`ubus call network.interface.lan status | jsonfilter -e '@["ipv4-address"][0].mask'`
LAN_DEV=`ubus call network.interface.lan status | jsonfilter -e '@["device"]'`
WAN_IP=`ubus call network.interface.wan status | jsonfilter -e '@["ipv4-address"][0].address'`
WAN_MASK=`ubus call network.interface.wan status | jsonfilter -e '@["ipv4-address"][0].mask'`
WAN_DEV=`ubus call network.interface.wan status | jsonfilter -e '@["device"]'`
echo "LAN_IP=$LAN_IP"
echo "LAN_MASK=$LAN_MASK"
echo "LAN_DEV=$LAN_DEV"
echo "WAN_IP=$WAN_IP"
echo "WAN_MASK=$WAN_MASK"
echo "WAN_DEV=$WAN_DEV"
root@OpenWrt:/# /tmp/test.sh 
LAN_IP=192.168.1.1
LAN_MASK=24
LAN_DEV=br-lan
WAN_IP=192.168.5.218
WAN_MASK=24
WAN_DEV=eth0


沒有留言: