Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script restarts the ConnectBox from Magenta, an ISP in Austria.
#!/usr/bin/env bash
#
# This script restarts the ConnectBox from Magenta, an ISP in Austria.
#
# Run this script with LOGIN_USER and LOGIN_PASS env variables set to the
# username and password of the router and it will restart it.
#
# LOGIN_USER is 'NULL' by default, that is also the default router setting.
#
# Example usage:
# LOGIN_PASS='correcthorsebatterystaple' ./connect_box_restart.sh
ROUTER_IP='192.168.0.1'
DEBUG=false # set to true to enable debug output
if [ -z "$LOGIN_USER" ]
then
# Default username is 'NULL'
LOGIN_USER='NULL'
fi
function checkStatus ()
{
if [[ "${1}" != '200 Ok' ]]
then
echo "Invalid response status: ${1}"
echo "${2}"
exit 1
fi
} # checkStatus
function parseCurlOut ()
{
HTTP_STATUS="$(echo "${1}" | grep "^< HTTP/1.1 " | cut -c 12- | rev | cut -c 2- | rev)"
SESSION_TOKEN="$(echo "${1}" | grep "< Set-Cookie: " | grep -o "sessionToken=[0-9]*" | grep -o "[0-9]*")"
if [ "$DEBUG" = true ]
then
echo "HTTP_STATUS: ${HTTP_STATUS}"
echo "SESSION_TOKEN: ${SESSION_TOKEN}"
echo
fi
} # parseCurlOut
function requestXmlSetter ()
{
CURL_OUT="$(curl "http://${ROUTER_IP}/xml/setter.xml" \
-H 'Accept: text/plain, */*; q=0.01' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "Cookie: sessionToken=${SESSION_TOKEN}" \
--data "token=${SESSION_TOKEN}&${1}" \
--compressed \
--insecure \
-s -v 2>&1)"
parseCurlOut "$CURL_OUT"
checkStatus "$HTTP_STATUS"
} # requestXmlSetter
function requestLoginPage ()
{
echo "Requesting login page ..."
CURL_OUT="$(curl "http://${ROUTER_IP}/common_page/login.html" -s -v 2>&1)"
parseCurlOut "$CURL_OUT"
checkStatus "$HTTP_STATUS" "$CURL_OUT"
} # requestLoginPage
function login ()
{
echo "Making login request ..."
if [ "$DEBUG" = true ]
then
echo "LOGIN_USER: ${LOGIN_USER}"
echo "LOGIN_PASS: ${LOGIN_PASS}"
fi
requestXmlSetter "fun=15&Username=${1}&Password=${2}"
} # login
function restartRouter ()
{
echo "Making restart request ..."
requestXmlSetter "fun=8"
} # restartRouter
requestLoginPage
login "$LOGIN_USER" "$LOGIN_PASS"
restartRouter
echo "ConnectBox successfully restarted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment