Skip to content

Instantly share code, notes, and snippets.

@dmcallejo
Last active January 2, 2023 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmcallejo/910e3b4f338b17166cd271fd2d015be6 to your computer and use it in GitHub Desktop.
Save dmcallejo/910e3b4f338b17166cd271fd2d015be6 to your computer and use it in GitHub Desktop.
Programatically reboots Genexis routers used by ISPs like Adamo in Spain.
#!/bin/sh
help()
{
echo "Usage: reboot_genexis [ -p | --password (REQUIRED) ]
[ -u | --user default: user]
[ -h | --host default: 192.168.1.1 ]
[ --help ]"
exit 2
}
SHORT=u:,p:,h:
LONG=user:,password:,host:,help
OPTS=$(getopt -a -n reboot_genexis --options $SHORT --longoptions $LONG -- "$@")
VALID_ARGUMENTS=$#
if [ "$VALID_ARGUMENTS" -eq 0 ]; then
help
fi
eval set -- "$OPTS"
while :
do
case "$1" in
-u | --user )
user="$2"
shift 2
;;
-p | --password )
password="$2"
shift 2
;;
-h | --host )
host="$2"
shift 2
;;
--help)
help
;;
--)
shift;
break
;;
*)
echo "Unexpected option: $1"
help
;;
esac
done
if [ -z "$password" ]
then
echo "A password is required for this script to work. Provide it with option -p <password> or --password <password>"
exit 1
fi
if [ -z "$user" ]
then
user="user"
fi
if [ -z "$host" ]
then
host="192.168.1.1"
fi
if ! [ -x "$(command -v jq)" ]
then
echo "ERROR: jq could not be found!"
exit 1
fi
if ! [ -x "$(command -v websocat)" ]
then
echo "ERROR: websocat could not be found! Download from https://github.com/vi/websocat and add it to PATH for this script to work."
exit 1
fi
if ! echo '{"jsonrpc":"2.0","method":"list","params":["6b8f3842a24b0524c68bba524e7051f4","*"],"id":1}' | websocat ws://"$host"/ -H Sec-WebSocket-Protocol:ubus-json -H Sec-WebSocket-Version:13 -1 -n > /dev/null 2>&1
then
echo "ERROR: Host $host invalid. Could not connect to Websocket"
exit 1
fi
rpc_session=$(echo '{"jsonrpc":"2.0","method":"call","params":["00000000000000000000000000000000","session","login",{"username":"'$user'","password":"'$password'"}],"id":1}' | websocat ws://"$host"/ -H Sec-WebSocket-Protocol:ubus-json -H Sec-WebSocket-Version:13 -1 -n | jq '.result[1].ubus_rpc_session')
if [ "$rpc_session" = "null" ]
then
echo "ERROR: Invalid user or password"
exit 1
fi
echo '{"jsonrpc":"2.0","method":"call","params":['$rpc_session',"juci.system","reboot",{}],"id":2}' | websocat ws://"$host"/ -H Sec-WebSocket-Protocol:ubus-json -H Sec-WebSocket-Version:13 -1 -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment