Skip to content

Instantly share code, notes, and snippets.

@danieltroger
Created November 23, 2014 18:22
Show Gist options
  • Save danieltroger/3b330e034801220302d2 to your computer and use it in GitHub Desktop.
Save danieltroger/3b330e034801220302d2 to your computer and use it in GitHub Desktop.
Gets the current downspeeed bandwith usage from a fritzbox
<?php
// worst unreadable shit but works
// dial #96*7* on your fritz box connected phone to get this working.
$h = fsockopen("fritz.box",23);
sleep(1);
fputs($h,"secretpass" . PHP_EOL);
stream_set_blocking($h,0);
$cmd = "echo \$((\$(ctlmgr_ctl r nqos settings/stat/ds_current_bps|sed 's/,.*//')*8 ))" . PHP_EOL;
$s = 0;
while(1)
{
$d = fgets($h);
if($d != false)
{
if($s == 2)
{
echo formatsize(str_replace("\n","",str_replace("\r","",$d))) . "/s\r";
$s = 0;
}
if($s)
{
$s = 2;
}
if($d == "# ")
{
fputs($h,$cmd);
$s = 1;
}
}
usleep(50000);
}
fclose($h);
function formatsize($size)
{
if($size < 1) return false;
if($size > 1024 * 1024 * 1024)
{
return round($size / 1024 / 1024 / 1024,2) . " GiB";
}
elseif($size > 1024 * 1024)
{
return round($size / 1024 / 1024,2) . " MiB";
}
elseif($size > 1024)
{
return round($size / 1024,2) . " KiB";
}
elseif($size < 1024)
{
return "{$size} Bytes";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment