luci-app-bmx6: add new methods to bmx6-info and clean/sanityze the code
Signed-off-by: Pau Escrich <p4u@dabax.net>
This commit is contained in:
parent
a881d5d4a6
commit
9646815e7d
1 changed files with 91 additions and 92 deletions
183
luci-app-bmx6/files/www/cgi-bin/bmx6-info
Normal file → Executable file
183
luci-app-bmx6/files/www/cgi-bin/bmx6-info
Normal file → Executable file
|
@ -1,119 +1,118 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# Copyright (C) 2011 Pau Escrich
|
# This script gives information about bmx6
|
||||||
# Contributors Jo-Philipp Wich <xm@subsignal.org>
|
# Can be executed from a linux shell: ./bmx6-info -s links
|
||||||
#
|
# Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# Special methods are tagged with '$', like $myself or $neighbours: http://host/cgi-bin/bmx6-info?$myself
|
||||||
# it under the terms of the GNU General Public License as published by
|
# When '$' is not used, raw bmx6 information from the filesystem is returned (/var/runb/bmx6/json/)
|
||||||
# the Free Software Foundation; either version 2 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along
|
|
||||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
||||||
#
|
|
||||||
# The full GNU General Public License is included in this distribution in
|
|
||||||
# the file called "COPYING".
|
|
||||||
#
|
|
||||||
# This script gives information about bmx6
|
|
||||||
# Can be executed from a linux shell: ./bmx6-info -s links
|
|
||||||
# Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
|
|
||||||
# If you ask for a directory you wil get the directory contents in JSON forman
|
|
||||||
|
|
||||||
BMX6_DIR="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR="/var/run/bmx6/json"
|
BMX6_DIR="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR="/var/run/bmx6/json"
|
||||||
|
|
||||||
#Checking if shell mode or cgi-bin mode
|
#Checking if shell mode or cgi-bin mode
|
||||||
if [ "$1" == "-s" ]; then
|
if [ "$1" == "-s" ]; then
|
||||||
QUERY="$2"
|
QUERY="$2"
|
||||||
else
|
else
|
||||||
QUERY="${QUERY_STRING%%=*}"
|
QUERY="${QUERY_STRING%%=*}"
|
||||||
echo "Content-type: application/json"
|
QUERY="${QUERY%%\?*}"
|
||||||
echo ""
|
QUERY="${QUERY%%\&*}"
|
||||||
|
echo "Content-type: application/json"
|
||||||
|
echo ""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
check_path() {
|
check_path() {
|
||||||
[ -d "$1" ] && path=$(cd $1; pwd)
|
[ -d "$1" ] && path=$(cd $1; pwd)
|
||||||
[ -f "$1" ] && path=$(cd $1/..; pwd)
|
[ -f "$1" ] && path=$(cd $1/..; pwd)
|
||||||
[ $(echo "$path" | grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
|
[ $(echo "$path" | grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
print_query() {
|
print_query() {
|
||||||
# If the query is a directory
|
# If the query is a directory
|
||||||
[ -d "$BMX6_DIR/$1" ] &&
|
[ -d "$BMX6_DIR/$1" ] &&
|
||||||
|
{
|
||||||
|
# If /all has not been specified
|
||||||
|
[ -z "$QALL" ] &&
|
||||||
|
{
|
||||||
|
total=$(ls $BMX6_DIR/$1 | wc -w)
|
||||||
|
i=1
|
||||||
|
echo -n "{ \"$1\": [ "
|
||||||
|
for f in $(ls $BMX6_DIR/$1); do
|
||||||
|
echo -n "{ \"name\": \"$f\" }"
|
||||||
|
[ $i -lt $total ] && echo -n ','
|
||||||
|
i=$(( $i + 1 ))
|
||||||
|
done
|
||||||
|
echo -n " ] }"
|
||||||
|
|
||||||
|
# If /all has been specified, printing all the files together
|
||||||
|
} || {
|
||||||
|
comma=""
|
||||||
|
echo -n "[ "
|
||||||
|
for entry in "$BMX6_DIR/$1/"*; do
|
||||||
|
[ -f "$entry" ] &&
|
||||||
{
|
{
|
||||||
# If /all has not been specified
|
${comma:+echo -n "$comma"}
|
||||||
[ -z "$QALL" ] &&
|
tr -d '\n' < "$entry"
|
||||||
{
|
comma=","
|
||||||
total=$(ls $BMX6_DIR/$1 | wc -w)
|
|
||||||
i=1
|
|
||||||
echo -n "{ \"$1\": [ "
|
|
||||||
for f in $(ls $BMX6_DIR/$1); do
|
|
||||||
echo -n "{ \"name\": \"$f\" }"
|
|
||||||
[ $i -lt $total ] && echo -n ','
|
|
||||||
i=$(( $i + 1 ))
|
|
||||||
done
|
|
||||||
echo -n " ] }"
|
|
||||||
|
|
||||||
# If /all has been specified, printing all the files together
|
|
||||||
} || {
|
|
||||||
comma=""
|
|
||||||
echo -n "[ "
|
|
||||||
for entry in "$BMX6_DIR/$1/"*; do
|
|
||||||
[ -f "$entry" ] &&
|
|
||||||
{
|
|
||||||
${comma:+echo "$comma"}
|
|
||||||
tr -d '\n' < "$entry"
|
|
||||||
comma=","
|
|
||||||
}
|
|
||||||
done
|
|
||||||
echo -n " ]"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
done
|
||||||
# If the query is a file, just printing the file
|
echo -n " ]"
|
||||||
[ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
|
}
|
||||||
|
}
|
||||||
|
# If the query is a file, just printing the file
|
||||||
|
[ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "${QUERY##*/}" == "all" ]; then
|
if [ "${QUERY##*/}" == "all" ]; then
|
||||||
QUERY="${QUERY%/all}"
|
QUERY="${QUERY%/all}"
|
||||||
QALL=1
|
QALL=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ "$QUERY" == '$myself' ]; then
|
||||||
|
hostname="$(cat /proc/sys/kernel/hostname)"
|
||||||
|
ip6="$(bmx6 -c show=status | grep ^BMX | awk '{print $5}')"
|
||||||
|
ip4="$(bmx6 -c show=status | grep ^BMX | awk '{print $6}')"
|
||||||
|
cidr6=$(lua -l luci.ip -e "ip=luci.ip.new(\"$ip6\"); print(ip:network():string()..'/'..ip:prefix())")
|
||||||
|
cidr4=$(lua -l luci.ip -e "ip=luci.ip.new(\"$ip4\"); print(ip:network():string()..'/'..ip:prefix())")
|
||||||
|
echo -n "{\"myself\":{\"hostname\":\"$hostname\",\"ip6\":\"$ip6\",\"ip4\":\"$ip4\",\"net6\":\"$cidr6\",\"net4\":\"$cidr4\"}}"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$QUERY" == '$info' ]; then
|
if [ "$QUERY" == '$info' ]; then
|
||||||
echo '{ "info": [ '
|
echo -n '{ "info": [ '
|
||||||
print_query status
|
print_query status
|
||||||
echo -n ","
|
echo -n ","
|
||||||
print_query interfaces
|
print_query interfaces
|
||||||
echo "] }"
|
echo -n "] }"
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$QUERY" == '$neighbours' ]; then
|
if [ "$QUERY" == '$neighbours' ]; then
|
||||||
QALL=1
|
QALL=1
|
||||||
echo '{ "neighbours": [ '
|
echo -n '{ "neighbours": [ '
|
||||||
echo '{ "originators": '
|
echo -n '{ "originators": '
|
||||||
print_query originators
|
print_query originators
|
||||||
echo '}, '
|
echo -n '}, '
|
||||||
echo '{ "descriptions": '
|
echo -n '{ "descriptions": '
|
||||||
print_query descriptions
|
print_query descriptions
|
||||||
echo "} ] }"
|
echo -n "} ] }"
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
else if [ "$QUERY" == '$tunnels' ]; then
|
|
||||||
bmx6 -c --jshow tunnels /r=0
|
|
||||||
exit 0
|
|
||||||
|
|
||||||
else
|
|
||||||
check_path "$BMX6_DIR/$QUERY"
|
|
||||||
print_query $QUERY
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ls -1F "$BMX6_DIR"
|
if [ "$QUERY" == '$tunnels' ]; then
|
||||||
|
bmx6 -c --jshow tunnels /r=0
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$QUERY" == "" ]; then
|
||||||
|
echo -n '{ "queries": ['
|
||||||
|
echo -n '{ "name": "$myself", "info": "basic network information of self node" },'
|
||||||
|
echo -n '{ "name": "$info", "info": "full network and device information of self node" },'
|
||||||
|
echo -n '{ "name": "$tunnels", "info": "accnouncements (tunnels) published by the mesh network" },'
|
||||||
|
echo -n '{ "name": "$neighbours", "info": "list of all my neighbours and their information" },'
|
||||||
|
echo -n '{ "name": "/", "info": "raw bmx6 json API" }]}'
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_path "$BMX6_DIR/$QUERY"
|
||||||
|
print_query $QUERY
|
||||||
|
|
||||||
|
#ls -1F "$BMX6_DIR"
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue