Load Fritz!Box scripts from remote website at boot time
When you want to run scripts on your Fritz!Box they need to be stored on the box, you would say. But since the Fritz!Box does not have a hard drive to store script files where do you store them? You can create them in the run-time file system but as the name implies that only exists at run-time and will be rebuild after a reboot.
It is possible to store your script in the debug.cfg file in the flash filesystem so that it will survive a reboot. But then you would always have to make modifications on the Fritz!Box and I would prefer to keep and maintain the files outside of the Fritz!Box but want to be able to easily update the files when needed.
So my solution is to store the scripts on my own web server and then have the Fritz!Box download these files at boot time. You can also modify the entire solution to use FTP if you want to, but I prefer this HTTP-based solution.
Part 1, the PHP script
To prepare create a folder on your web server, such as /fritz to contain the scripts and place a .htaccess file in the folder to keep public users out. Only the PHP script can read the files and show them when the correct parameters are provided. Contents of the .htaccess file:
deny from all
Next place this script on your web server:
<?php
$accesscode = $_GET['accesscode'];
$filename = $_GET['filename'];
if ($accesscode = "yourcodegoeshere")
{
$lines = file("fritz/".$filename);
foreach ($lines as $line) {
echo $line;
}
}
?>
The name I have used for the PHP-script in the remainder of this example is fritzdownload.php.
Part 2, the script on the Fritz!Box
This script downloads the correct file from the web server and executes it. I prefer to use a script that continuously runs on the server inside a while loop, check the other scripts on my web site.
The contents of this script should go in a file called /var/flash/debug.cfg You can not edit it directly so either create your own file and use this command to place it in the file:
cat yourfile > /var/flash/debug.cfg
Or modify the debug.cfg file with nvi, a wrapper for vi that allows you to modify the files that are n the flash file system.
#Script tested for Fritz!Box 7390
#Rob Bastiaansen
#January 2012
#mail@robbastiaansen.nl
#Script to download a script from a website and run that script on the Fritz!Box
#Using this setup you can easily maintain your script and keep it safe on a remote
#location without the need to update files on your Fritz!Box. Also any modifications
#to the Fritz!Box are normally gone when you reboot. You could place your script in the
#file in the flash file system, but then you would also have to maintain it on the
#Fritz!Box. With this procedure you can place this script you are reading now
#in the flash filesystem and never have to update it again because your actual script
#is stored on a remote server. Could have used FTP, there is a ftpget command in
#busybox, it would be a small modification if you want to go that way.
#We don't just download a text file from the website, you could do that of course but
#then anyone could download your script. So for extra security I have created a PHP
#script that prints the contents of a text file you specify (containing the script) as html
#so we can download it with wget. The additional security is in the fact that the PHP
#script only prints the text file when the correct parameter and value are provided for
#both the access code as well as the file name. For security reasons I would still never
#store plain text passwords in the script that you download
#replace the values here to work in your own environment
website="www.yourwebsite.com"
filepath="/fritzdownload.php"
#use the fritzname parameter to identify the Fritz!Box, useful when managing multiple boxes. Use this as the file name on your web server.
#use the fritzname parameter to identify the Fritz!Box, useful when managing multiple boxes. Use this as the file name on your web server.
export fritzname=fritzhome
echo download script $fritzname.sh>> /tmp/fritzboot.log
parameter="?accesscode=youraccesscode&filename=$fritzname.sh"
#log when the Fritz!Box was started and later send that bootlog via email
echo $(date) Fritz!Box $fritzname started >> /tmp/fritzboot.log
connected=0
#use this loop to wait until we have an actual internet connection
while [ $connected -eq 0 ]
do
if !(ping $website -c 1) then
echo $(date) $fritzname not connected to the internet >> /tmp/fritzboot.log
sleep 5
else
connected=1
fi
done
done
#now that we have an internet connection we can download the script from the
#website and execute it
wget http://$website$filepath$parameter -O /tmp/tempscript.sh >> /tmp/fritzboot.log
#when downloading extra carriage return characters are at the end of each line that need to be removed
tr -d '\r' < /tmp/tempscript.sh > /var/fritzscript.sh
chmod +x /var/fritzscript.sh
echo $(date) Fritz!Box $fritzname started and going to start downloaded script >> /tmp/fritzboot.log
#email the boot log so that we know what is going on in the Fritz!Box
mailer -s "FritzBox $fritzname bootlog" -f youraddress@somehwere.com -t youraddress@somehwere.com -m smtp.yourdomain.com-i /tmp/fritzboot.log
#and finally execute the script
exec /var/fritzscript.sh




