Fritzbox: fetch data with a bash script

Fritzbox is a popular router with many great features. Today I want share a script that allows you to extract some information without using the web interface. Many thanks to home.debian-hell.org. This post inspred me to look into this topic and I reused the connection part.

#!/bin/bash
 
_FBOX="https://$1"
_HTTP_USERNAME="$2"
_HTTP_PASSWORD="$3"
_USERNAME=""
_PASSWORD=""
_EXPORT_PASSWORD="foobar"
echo "Fritzbox URL: ${_FBOX}"

 
# get challenge key from FB
_CHALLENGE=$(curl -s \
                  -k \
                  --user ${_HTTP_USERNAME}:${_HTTP_PASSWORD} \
                  "${_FBOX}/login.lua" | \
              egrep "^g_challenge|ready.onReady\(function" | \
              tail -1 | \
              awk -F'"' '{ print $2 }')
#echo "challenge: ${_CHALLENGE}"
 
# build md5 from challenge key and password
_MD5=$(echo -n \
            ${_CHALLENGE}"-"${_PASSWORD} | \
        iconv -f ISO8859-1 \
              -t UTF-16LE | \
        md5sum -b | \
        awk '{print substr($0,1,32)}') 
#echo "md5: ${_MD5}"

# assemble challenge key and md5
_RESPONSE=${_CHALLENGE}"-"${_MD5}
#echo "response: ${_RESPONSE}"

# get sid for later use
_SID=$(curl -i \
            -s \
            -k \
            --user ${_HTTP_USERNAME}:${_HTTP_PASSWORD} \
            -d 'response='${_RESPONSE} \
            -d 'page=' \
            -d 'username='${_USERNAME} \
            ${_FBOX}/login.lua | \
        grep "Location:" | \
        awk -F'=' {' print $NF '}) 
#echo "sid: ${_SID}"

# get configuration from FB and write to a file
curl -s \
           -k \
           --user ${_HTTP_USERNAME}:${_HTTP_PASSWORD} \
           --form 'sid='${_SID} \
           --form 'ImportExportPassword='${_EXPORT_PASSWORD} \
           --form 'ConfigExport=' \
           ${_FBOX}/cgi-bin/firmwarecfg > tmp_$1_export.txt

echo "telefone numbers: "
grep "name = \"0" tmp_$1_export.txt | sed -e 's/.*= "//' -e 's/";//'

echo "ssid:" 
grep "ssid = " tmp_$1_export.txt | grep -v "_ssid" | sed -e 's/.*= "//' -e 's/";//'

curl -s \
           -k \
           --user ${_HTTP_USERNAME}:${_HTTP_PASSWORD} \
           "${_FBOX}/cgi-bin/webcm?getpage=../html/de/menus/menu2.html&var:menu=fon&var:pagename=foncalls&sid=${_SID}" > tmp_$1_calls.txt
echo Calls:
grep '(TrCall(' tmp_$1_calls.txt | sed -e 's/.*(//' -e 's/"//g' -e 's/^1/incomming/' -e 's/^3/outgoing/' | cut -d "," -f 1,2,3,4,8

curl -s \
           -k \
           --user ${_HTTP_USERNAME}:${_HTTP_PASSWORD} \
           ${_FBOX}/wlan/encrypt.lua?sid=${_SID} > tmp_$1_wlan_encrypt.txt
echo WLAN key: ${_WLANKEY}
grep 'settings/pskvalue' tmp_$1_wlan_encrypt.txt | sed -e 's/.* = //' -e 's/"//g' -e 's/, $//'

rm tmp_$1*

you can start the script simply with ./fritzbox.sh 10.20.30.40 myusername mypassword

You might think that it is not very useful to extract the last calls to a text file on your server and you are right 🙂
The output is just an example and should give you an overview how it works and what would be possible.

You can store the information somewheare and compare them one a week for automatic email notifications in case a new port forwarding rule was created.
Have fun! 🙂

Tagged