Skip to content

Instantly share code, notes, and snippets.

@pjvds
Last active December 14, 2023 16:30
Show Gist options
  • Save pjvds/b88f51a3d5f6e5f0718a to your computer and use it in GitHub Desktop.
Save pjvds/b88f51a3d5f6e5f0718a to your computer and use it in GitHub Desktop.
# This script resets the AVM FritzBox router
#
# The procedure is as follow:
# 1. GET request logincheck.lua
# 2. grep the session id from the location header
# 3. POST request reboot.lua with session id
# 4. grep the potentialy new session id from the location header
# 5. GET request on reboot.lua with session id
echo "looking for fritz.box"
if ! ping -c1 fritz.box &>/dev/null; then
echo "couldn't find fritz.box"
exit
fi
echo "fritz.box found"
echo "retrieving session id $SID"
SID=$(curl -s -I "http://fritz.box/logincheck.lua" | grep -Fi Location | awk -F'[=]' '{print $2}')
SLEEP 1
echo "session id $SID"
echo "requesting reboot"
SID=$(curl -s -i -H "Content-Type: application/x-www-form-urlencoded" -H "Origin: http://fritz.box" -H "Referer: http://fritz.box/system/reboot.lua\?sid\=$SID" --data "reboot=&sid=$SID" -L http://fritz.box/system/reboot.lua | grep -Fi Location | awk -F'[=]' '{print $2}')
echo "new session id $SID"
SLEEP 1
echo "requesting status"
curl http://fritz.box/reboot.lua?ajax=1&sid=$REBOOT_SID
SLEEP 1
echo "waiting for fritzbox to go down"
while ping -c1 www.google.com &>/dev/null; do : sleep 1; done
echo "FRITZ!Box down"
SLEEP 1
echo "waiting for fritzbox to come up"
while ! ping -c1 www.google.com &>/dev/null; do : sleep 1; done
echo "FRITZ!Box up"
@Bergerie
Copy link

I'm trying to implement the above script to allow for scheduled reset of a remotely located Fritzbox. Unfortunately, the script terminates prematurely. Could you pls give me a lead what to do to make it working.
Result:

looking for fritz.box
fritz.box found
retrieving session id
./reset_fritzbox.sh: line 21: SLEEP: command not found
session id
requesting reboot
new session id
./reset_fritzbox.sh: line 28: SLEEP: command not found
requesting status
./reset_fritzbox.sh: line 32: SLEEP: command not found
waiting for fritzbox to go down
{"pid":"logout"}

end Result

@orangebutblue
Copy link

@Bergerie you need to replace 'SLEEP' with 'sleep'.

However this script still doesn't work for me.

looking for fritz.box fritz.box found retrieving session id session id requesting reboot new session id requesting status {"pid":"logout"}waiting for fritzbox to go down

then it waits there forever. I'm guessing the fritzbox isn't eben being told to restart, so he keeps silently pinging google until the internet goes down - which never happens.

How is this script even supposed to restart the router without any authentication?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment