I like to post here a mini REST/HTTP server based on NetCat for execute ANY arbitrary script in your environment.
I created this for call the good libAlert using simple HTTP requests (using CURL from any other device). This is best than use TELNET or SSH, as it don't needs any other external command in the remote device. Also is fast. Obviously, it has limitations: no secure, and BLOCKING (only one client at time). However it works robust!
You only need to condifure the script, copy in your environment, and start the server from your etc/init.d directory (I suggest to create a custom mini-rest-server.init). Perhaps you like to incorporate this in future versions of SamyGO!
Here the script...
mini-rest-server.sh:
Code: Select all
#!/mnt/bin/bash
#
# Simple REST/HTTP Command server based on NetCat (non multithreaded!)
# (c) 2015,Manos78 @ XDA
# Based on: http://stackoverflow.com/questions/26455434/create-a-minimum-rest-web-server-with-netcat-nc
#
# Configure for your environment!
TMP_DIR=/tmp
SERVER_PORT=5500
SODIR=/mnt/opt/privateer/usr/so
# Internal vars
FILENAME=${0##*/}
BASENAME=${FILENAME%%.*}
PID_FILE=$TMP_DIR/$BASENAME.pid
OUT_FILE=$TMP_DIR/$BASENAME.out
NCP_FILE=$TMP_DIR/$BASENAME.ncp
#echo $PID_FILE
#echo $OUT_FILE
#echo $NCP_FILE
# Controlled exit
trap "rm -f $PID_FILE; rm -f $OUT_FILE; rm -rf $NCP_FILE; exit" EXIT SIGINT
# Start of script
rm -f $PID_FILE
rm -f $OUT_FILE
rm -f $NCP_FILE
mkfifo $OUT_FILE || exit
touch $NCP_FILE || exit
pidfile() { (
echo $BASHPID > "$1"
shift
exec "$@"
) }
echo "* Starting with pid: $$"
echo $$ > $PID_FILE
while true
do
echo "executing netcat service: listening (oo sec.) for incoming requests"
# On plain NetCat inside Busybox use simple parameters
#cat "$OUT_FILE" | pidfile $NCP_FILE nc -vvvv -n -l -p $SERVER_PORT > >( # parse the netcat output, to build the answer redirected to the pipe "$OUT_FILE".
cat "$OUT_FILE" | pidfile $NCP_FILE nc -l -p $SERVER_PORT > >( # parse the netcat output, to build the answer redirected to the pipe "$OUT_FILE".
export REQUEST=
while read line
do
line=$(echo "$line" | tr -d '[\r\n]')
if echo "$line" | grep -qE '^GET /' # if line starts with "GET /"
then
REQUEST=$(echo "$line" | cut -d ' ' -f2) # extract the request
elif [ "x$line" = x ] # empty line / end of request
then
HTTP_200="HTTP/1.1 200 OK"
HTTP_404="HTTP/1.1 404 Not Found"
HTTP_404="HTTP/1.1 400 Bad Request"
HTTP_LOCATION="Location:"
# call a script here
# Note: REQUEST is exported, so the script can parse it (to answer 200/403/404 status code + content)
# Testing echo service
if echo "$REQUEST" | grep -qE '^/service/test/echo/'
then
printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" "$REQUEST" ${REQUEST#"/echo/"} > "$OUT_FILE"
# Testing data service
elif echo "$REQUEST" | grep -qE '^/service/test/date'
then
date > "$OUT_FILE"
# Testing stats service
elif echo "$REQUEST" | grep -qE '^/service/test/stats'
then
vmstat -S M > "$OUT_FILE"
# Your "message" service (using libAlert)
elif echo "$REQUEST" | grep -qE '^/service/message/'
then
IFS='/'
array=( $REQUEST )
type=${array[3]^^}
text=${array[4]}
blink=${array[5]^^}
blinks=${array[6]}
delay=${array[7]^^}
delays=${array[8]}
if ! echo $type | grep -qEx '^TEXT|MSGBOX|SYSTEM|CENTER' ;
then
printf "%s\n%s %s\n\n%s\n" "$HTTP_400" "$HTTP_LOCATION" "$REQUEST" "REST API: Bad Request!" > "$OUT_FILE"
else
if ! echo $blink | grep -qEx '^BLINK' ;
then
blink=""
blinks=""
fi
if ! echo $delay | grep -qEx '^DELAY' ;
then
delay=""
delays=""
fi
echo "call $SODIR/libAlert $type:\"$text\" $blink:$blinks $delay:$delays"
"$SODIR"/libAlert.sh $type:\"$text\" $blink:$blinks $delay:$delays
printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" "$REQUEST" "REST API: OK" > "$OUT_FILE"
fi
# Unkown request!
else
printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" "$REQUEST" "Resource $REQUEST NOT FOUND!" > "$OUT_FILE"
fi
# Restart and Close
echo "closing connection... "
kill -SIGINT $(cat "$NCP_FILE")
fi
done
)
done