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"
21 # workaround to support old format starting with '$'
22 QUERY
="$(echo "$QUERY" | sed s/'\$'//)"
25 [ -d "$1" ] && path
=$
(cd $1; pwd)
26 [ -f "$1" ] && path
=$
(cd $1/..
; pwd)
27 [ $
(echo "$path" |
grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
31 # If the query is a directory
32 [ -d "$BMX6_DIR/$1" ] &&
34 # If /all has not been specified
37 total
=$
(ls $BMX6_DIR/$1 |
wc -w)
39 echo -n "{ \"$1\": [ "
40 for f
in $
(ls $BMX6_DIR/$1); do
41 echo -n "{ \"name\": \"$f\" }"
42 [ $i -lt $total ] && echo -n ','
47 # If /all has been specified, printing all the files together
51 for entry
in "$BMX6_DIR/$1/"*; do
54 ${comma:+echo -n "$comma"}
62 # If the query is a file, just printing the file
63 [ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
66 if [ "${QUERY##*/}" == "all" ]; then
71 if [ "$QUERY" == 'myself' ]; then
72 hostname
="$(cat /proc/sys/kernel/hostname)"
73 ip6
="$(bmx6 -c show=status | grep ^BMX | awk '{print $5}')"
74 ip4
="$(bmx6 -c show=status | grep ^BMX | awk '{print $6}')"
75 cidr6
=$
(lua
-l luci.ip
-e "ip=luci.ip.new(\"$ip6\"); print(ip:network():string()..'/'..ip:prefix())")
76 cidr4
=$
(lua
-l luci.ip
-e "ip=luci.ip.new(\"$ip4\"); print(ip:network():string()..'/'..ip:prefix())")
77 echo -n "{\"myself\":{\"hostname\":\"$hostname\",\"ip6\":\"$ip6\",\"ip4\":\"$ip4\",\"net6\":\"$cidr6\",\"net4\":\"$cidr4\"}}"
81 if [ "$QUERY" == 'info' ]; then
82 echo -n '{ "info": [ '
85 print_query interfaces
90 if [ "$QUERY" == 'neighbours' ]; then
92 echo -n '{ "neighbours": [ '
93 echo -n '{ "originators": '
94 print_query originators
96 echo -n '{ "descriptions": '
97 print_query descriptions
102 if [ "$QUERY" == 'tunnels' ]; then
103 bmx6
-c --jshow tunnels
/r
=0
107 if [ "$QUERY" == "" ]; then
108 echo -n '{ "queries": ['
109 echo -n '{ "name": "myself", "info": "basic network information of self node" },'
110 echo -n '{ "name": "info", "info": "full network and device information of self node" },'
111 echo -n '{ "name": "tunnels", "info": "accnouncements (tunnels) published by the mesh network" },'
112 echo -n '{ "name": "neighbours", "info": "list of all my neighbours and their information" },'
113 echo -n '{ "name": "/", "info": "raw bmx6 json API" }]}'
117 check_path
"$BMX6_DIR/$QUERY"