wakeboard/handle_requests.sh

64 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
if [[ "$1" =~ ^refresh ]]; then
ip=$(echo "$1" | cut -d '/' -f2)
body="{\n\"time\": \"$(date +%T)\",\n"
body+="\"status\": \"unknown\"\n}\n"
cat <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$(echo -ne $body)
EOF
echo -ne "$body" > status/$ip
ret=1
for i in {0..30}; do
ping -c 1 -w 5 -t 1 -q "$ip"
ret=$?
[ $ret -eq 0 ] && break
done
echo -e "{\n\"time\": \"$(date +%T)\"," > status/$ip
if [ $ret -eq 0 ]; then
echo -e "\"status\": \"up\"\n}\n" >> status/$ip
else
echo -e "\"status\": \"down\"\n}\n" >> status/$ip
fi
exit 0
fi
if [[ "$1" =~ ^status ]]; then
ip=$(echo "$1" | cut -d '/' -f2)
if [ ! -r "status/$ip" ]; then
body="{\n\"status\": \"unknown\"\n}\n"
else
body=$(cat status/$ip)
fi
cat <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -e "$body" | wc -c))
$body
EOF
exit 0
fi
if [[ "$1" =~ ^wol ]]; then
mac=$(echo "$1" | cut -d '/' -f2)
wol "$mac" > /dev/null
body="{\"response\": \"ok\"}\n"
cat <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$(echo -ne $body)
EOF
exit 0
fi
exit 1