I made a Bash-script a while ago to grab the DSL-line information from my Fritz!box 7360.
The internet company was doing some maintenance (speed upgrades yay!) and i was looking for an easy way to check the speeds that the modem trained into on the DSL-line.
#!/bin/bash
#Credits go to http://www.administrator.de/contentid/214598 "Informationen aus dem Webinterface einer Fritz!Box via Shellscript auslesen"
#Script below was tested on Fritz!box 7360 with OS 6.30
_BOXURL="http://fritz.box"
_USERNAME=""
_PASSWORD="YOURPASSWORDHERE"
_CHALLENGE=$(curl -s ${_BOXURL}/login.lua | grep "^g_challenge" | awk -F '"' '{ print $2 }')
_MD5=$(echo -n ${_CHALLENGE}"-"${_PASSWORD} | iconv -f ISO8859-1 -t UTF-16LE | md5sum -b | awk '{print substr($0,1,32)}')
_RESPONSE="${_CHALLENGE}-${_MD5}"
_SID=$(curl -i -s -k -d 'response='${_RESPONSE} -d 'page=' -d "username=${_USERNAME}" "${_BOXURL}/login.lua" | grep "Location:" | grep -Poi 'sid=[a-f\d]+' | cut -d '=' -f2)
#DSL-Informationen zur Leitungsqualität
_PAGE_DSL_STATS=$(curl -s ${_BOXURL}/internet/dsl_stats_tab.lua -d 'sid='${_SID})
_DSLAM_DOWN=$(echo "${_PAGE_DSL_STATS}" | awk 'match($0,/Max. DSLAM throughput.*"c3">([0-9]+)<\/td>.*Min. DSLAM throughput/,arr){print arr[1]};')
_DSLAM_UP=$(echo "${_PAGE_DSL_STATS}" | awk 'match($0,/Max. DSLAM throughput.*"c4">([0-9]+)<\/td>.*Min. DSLAM throughput/,arr){print arr[1]};')
_ATTAINABLE_DOWN=$(echo "${_PAGE_DSL_STATS}" | awk 'match($0,/Attainable throughput.*"c3">([0-9]+)<\/td>.*Current throughput/,arr){print arr[1]};')
_ATTAINABLE_UP=$(echo "${_PAGE_DSL_STATS}" | awk 'match($0,/Attainable throughput.*"c4">([0-9]+)<\/td>.*Current throughput/,arr){print arr[1]};')
_CURRENT_DOWN=$(echo "${_PAGE_DSL_STATS}" | awk 'match($0,/Current throughput.*"c3">([0-9]+)<\/td>.*Seamless rate adaptation/,arr){print arr[1]};')
_CURRENT_UP=$(echo "${_PAGE_DSL_STATS}" | awk 'match($0,/Current throughput.*"c4">([0-9]+)<\/td>.*Seamless rate adaptation/,arr){print arr[1]};')
#Convert kbit to Mbit with .1 decimal point
dslam_down_mbit=$(echo "scale=1; $_DSLAM_DOWN/1000" | bc)
dslam_up_mbit=$(echo "scale=1; $_DSLAM_UP/1000" | bc)
att_down_mbit=$(echo "scale=1; $_ATTAINABLE_DOWN/1000" | bc)
att_up_mbit=$(echo "scale=1; $_ATTAINABLE_UP/1000" | bc)
current_down_mbit=$(echo "scale=1; $_CURRENT_DOWN/1000" | bc)
current_up_mbit=$(echo "scale=1; $_CURRENT_UP/1000" | bc)
echo "Max. DSLAM throughput - Down: $_DSLAM_DOWN kbit/s ("$dslam_down_mbit" Mbit) "
echo "Max. DSLAM throughput - Up: $_DSLAM_UP kbit/s ("$dslam_up_mbit" Mbit) "
echo ""
echo "Attainable throughput - Down: $_ATTAINABLE_DOWN kbit/s ("$att_down_mbit" Mbit) "
echo "Attainable throughput - Up: $_ATTAINABLE_UP kbit/s ("$att_up_mbit" Mbit) "
echo ""
echo "Current throughput - Download: $_CURRENT_DOWN kbit/s ("$current_down_mbit" Mbit) "
echo "Current throughput - Upload: $_CURRENT_UP kbit/s ("$current_up_mbit" Mbit) "
Change YOURPASSWORDHERE
to the password of your Fritz!box, save the script (i named it speeds.sh
and run it like this bash speeds.sh
or whatever you have called it. This will give you a result like this:
Max. DSLAM throughput - Down: 52000 kbit/s (52.0 Mbit)
Max. DSLAM throughput - Up: 5224 kbit/s (5.2 Mbit)
Attainable throughput - Down: 61961 kbit/s (61.9 Mbit)
Attainable throughput - Up: 29780 kbit/s (29.7 Mbit)
Current throughput - Download: 51997 kbit/s (51.9 Mbit)
Current throughput - Upload: 5223 kbit/s (5.2 Mbit)
Putting the values into HASS by sending them to a virtual sensor is then only one step away, quite simple by using cURL Put the script in a crontab to run every x minutes and off you go! Be sure to include the full location of bash
when you put it into crontab. You can find it by using which bash
.
If you are interested in other values, just fiddle around with the awk
, grep
and cut
parts.
The script was tested on my Fritz!box 7360 with firmware 6.30.