Skip to content

Instantly share code, notes, and snippets.

@vuolter
Last active September 28, 2024 07:58
Show Gist options
  • Save vuolter/70fdd9a0b1a8c57026951a159e1d65e0 to your computer and use it in GitHub Desktop.
Save vuolter/70fdd9a0b1a8c57026951a159e1d65e0 to your computer and use it in GitHub Desktop.
Reboot Grandstream ATA
#!/opt/bin/bash
#
# gsreboot.sh v2.1 - Script to reboot Grandstream ATA
#
# @author: Walter Purcaro <[email protected]>
# @dependencies: bash coreutils-base64 curl grep logger sed
#
###############################################################################
name="$(basename "$0")[$$]"
rebooting=false
quiet=false
COOKIEFILE="/tmp/cookies.txt"
MAXREDIRS=10
gs_echo () {
echo "$1"
if ! $quiet; then
logger -t "$name" "$1"
fi
}
gs_error () {
echo "ERROR: $1"
logger -t "$name" -p "error" "$1"
exit 1
}
gs_version() {
echo "gsreboot v2.1 by Walter Purcaro <[email protected]>"
echo "https://gist.github.com/vuolter/70fdd9a0b1a8c57026951a159e1d65e0"
}
gs_help() {
if [ -n "$1" ]; then
echo "$1"
logger -t "$name" -p "error" "$1"
else
echo "Script to reboot Grandstream ATA"
fi
echo
echo -e "Usage: $name [-q] [-v] [-h] <ip> <username> <password>"
echo -e " -q \t\t\t Silent mode (shows only exit status message, no syslog logging)"
echo -e " -v \t\t\t Print version info and exit"
echo -e " -h \t\t\t Print usage info and exit"
echo
}
gs_getopts () {
OPTSTRING="qh"
while getopts $OPTSTRING opt; do
case "$opt" in
q)
quiet=true
;;
v)
gs_version
exit 2
;;
h)
gs_help
exit 2
;;
:)
gs_help "Option -$OPTARG requires an argument"
exit 1
;;
?)
gs_help "Invalid option: -$OPTARG"
exit 1
;;
esac
done
shift $((OPTIND - 1))
ip="$1"
if [ "$#" -ne 3 ]; then
if [ -z "$1" ]; then
msg="No argument IP address provided"
elif [ -z "$2" ]; then
msg="No argument username provided"
elif [ -z "$3" ]; then
msg="No argument password provided"
fi
gs_help "$msg"
exit 1
fi
user="$2"
pw="$(echo -n "$3" | base64)"
}
gs_login () {
code="$(curl \
-fsS \
-X POST \
-c "$COOKIEFILE" \
-d "username=$user" \
-d "P2=$pw" \
-d "Login=Login" \
-d "gnkey=0b82" \
-o /dev/null \
-w "%{http_code}" \
--url "http://$ip/cgi-bin/dologin" \
--max-redirs "$MAXREDIRS")"
if [ "${code:0:1}" = 2 ]; then
session="$(
curl -fsS -b "$COOKIEFILE" --url "http://$ip/cgi-bin/gr909" --max-redirs "$MAXREDIRS" |
grep -E -o -m 1 'session_token.*value="[0-9a-z]+"' |
sed -r 's/.*"([0-9a-z]+)".*/\1/'
)"
fi
if [ -z "$session" ] && ! $quiet; then
gs_error "Cannot login to Grandstream device $ip"
fi
}
gs_reboot () {
rebooting=$(
curl \
-fsS \
-X POST \
-b "$COOKIEFILE" \
-d "session_token=$session" \
--url "http://$ip/cgi-bin/rs" \
--max-redirs "$MAXREDIRS" |
grep -q 'The device is rebooting now' &&
echo true ||
echo false
)
if ! $rebooting && ! $quiet; then
gs_error "Cannot reboot Grandstream device $ip"
fi
}
gs_exit () {
rm -f "$COOKIEFILE"
if $rebooting; then
gs_echo "Restarting Grandstream device $ip"
else
gs_error "Cannot restart Grandstream device $ip"
fi
}
trap gs_exit EXIT
gs_getopts "$@"
gs_login
gs_reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment