6518c8da4f04cb602cbfb976deac7bf29b52cb04
2 # This script gives information about bmx6
3 # Can be executed from a linux shell: ./bmx6-info -s links
4 # Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
5 # Special methods are tagged with '$', like $myself or $neighbours: http://host/cgi-bin/bmx6-info?$myself
6 # When '$' is not used, raw bmx6 information from the filesystem is returned (/var/runb/bmx6/json/)
8 BMX6_DIR
="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR
="/var/run/bmx6/json"
10 #Checking if shell mode or cgi-bin mode
11 if [ "$1" == "-s" ]; then
14 QUERY
="${QUERY_STRING%%=*}"
17 echo "Content-type: application/json"
22 [ -d "$1" ] && path
=$
(cd $1; pwd)
23 [ -f "$1" ] && path
=$
(cd $1/..
; pwd)
24 [ $
(echo "$path" |
grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
28 # If the query is a directory
29 [ -d "$BMX6_DIR/$1" ] &&
31 # If /all has not been specified
34 total
=$
(ls $BMX6_DIR/$1 |
wc -w)
36 echo -n "{ \"$1\": [ "
37 for f
in $
(ls $BMX6_DIR/$1); do
38 echo -n "{ \"name\": \"$f\" }"
39 [ $i -lt $total ] && echo -n ','
44 # If /all has been specified, printing all the files together
48 for entry
in "$BMX6_DIR/$1/"*; do
51 ${comma:+echo -n "$comma"}
59 # If the query is a file, just printing the file
60 [ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
63 if [ "${QUERY##*/}" == "all" ]; then
68 if [ "$QUERY" == '$myself' ]; then
69 hostname
="$(cat /proc/sys/kernel/hostname)"
70 ip6
="$(bmx6 -c show=status | grep ^BMX | awk '{print $5}')"
71 ip4
="$(bmx6 -c show=status | grep ^BMX | awk '{print $6}')"
72 cidr6
=$
(lua
-l luci.ip
-e "ip=luci.ip.new(\"$ip6\"); print(ip:network():string()..'/'..ip:prefix())")
73 cidr4
=$
(lua
-l luci.ip
-e "ip=luci.ip.new(\"$ip4\"); print(ip:network():string()..'/'..ip:prefix())")
74 echo -n "{\"myself\":{\"hostname\":\"$hostname\",\"ip6\":\"$ip6\",\"ip4\":\"$ip4\",\"net6\":\"$cidr6\",\"net4\":\"$cidr4\"}}"
78 if [ "$QUERY" == '$info' ]; then
79 echo -n '{ "info": [ '
82 print_query interfaces
87 if [ "$QUERY" == '$neighbours' ]; then
89 echo -n '{ "neighbours": [ '
90 echo -n '{ "originators": '
91 print_query originators
93 echo -n '{ "descriptions": '
94 print_query descriptions
99 if [ "$QUERY" == '$tunnels' ]; then
100 bmx6
-c --jshow tunnels
/r
=0
104 if [ "$QUERY" == "" ]; then
105 echo -n '{ "queries": ['
106 echo -n '{ "name": "$myself", "info": "basic network information of self node" },'
107 echo -n '{ "name": "$info", "info": "full network and device information of self node" },'
108 echo -n '{ "name": "$tunnels", "info": "accnouncements (tunnels) published by the mesh network" },'
109 echo -n '{ "name": "$neighbours", "info": "list of all my neighbours and their information" },'
110 echo -n '{ "name": "/", "info": "raw bmx6 json API" }]}'
114 check_path
"$BMX6_DIR/$QUERY"