Hallo zusammen,
ich möchte meine WLAN Geräte über die Fritzbox automatisiert auslesen.
Im Moment mache ich das so:
$client = new SoapClient(
null,
array(
'location' => "http://".$fritzboxIP.":".$fritzboxPort."/upnp/control/wlanconfig2",
'uri' => "urn:dslforum-org:service:WLANConfiguration:2",
'noroot' => True,
'trace' => False,
'login' => $login,
'password' => $password
)
);
$NumberOfHosts = $client->GetTotalAssociations();
for ($i=0;$i<$NumberOfHosts;$i++) {
$Host = $client->GetGenericAssociatedDeviceInfo(new SoapParam($i,'NewAssociatedDeviceIndex'));
$Hosts[] = $Host;
}
Leider werden hier nur die 5GHz Geräte angezeigt.
Durch stöbern im Internet bin ich auf folgenden Thread gestoßen:
FritzBox mit SOAP auslesen und steuern
Also habe ich Symcon installiert: Installation IP-Symcon :: Automatisierungssoftware
um folgendes Script zu starten:
<?php
set_time_limit(240);
$fbroot ='http://192.168.178.1:49000';
$fb_user='dslf-config';
$fb_password='XXXXXXXXXXXX';
$descXMLs = array(
"tr64desc.xml",
"igddesc.xml",
//"MediaServerDevDesc.xml", //gibt es bei mir nicht
//"onlinestoredesc.xml", //gibt es bei mir nicht
//"fboxdesc.xml", //gibt es bei mir nicht
"usbdesc.xml"
);
// ab hier nichts ändern
foreach ($descXMLs as $descXML)
{
$xml = @simplexml_load_file($fbroot.'/'.$descXML);
if ($xml === false)
{
echo "Not found:".$descXML.PHP_EOL;
continue;
}
$xml->registerXPathNamespace('fritzbox', $xml->getNameSpaces(false)[""]);
$xmlservices = $xml->xpath('//fritzbox:service');
foreach ($xmlservices as $service)
{
$serviceIdent=str_replace(array(".",":","-","_"),array("","","",""),(string)$service->serviceId);
if (!ctype_alnum($serviceIdent))
{
echo "ERROR: Konnte Dienst nicht hinzufügen. Name kann nicht als Ident verwendet werden:".(string)$service->serviceId.PHP_EOL;
continue;
}
$services[$serviceIdent]['descXML'] = $descXML;
$services[$serviceIdent]['uri'] = (string)$service->serviceType;
$services[$serviceIdent]['location'] =$fbroot.(string)$service->controlURL;
$services[$serviceIdent]['SCPDURL'] =trim((string)$service->SCPDURL,"/");
}
}
$parent = IPS_GetParent($_IPS['SELF']);
foreach ($services as $serviceIdent=>$data)
{
$StateVariables = array();
$service_id = @IPS_GetObjectIDByIdent($serviceIdent,$parent);
if ($service_id===false)
{
$service_id = IPS_CreateCategory();
IPS_SetIdent($service_id, $serviceIdent);
IPS_SetParent($service_id, $parent);
IPS_SetName($service_id,$data['SCPDURL']);
IPS_SetInfo($service_id,$data['uri']);
}
$xmlDesc = @simplexml_load_file($fbroot.'/'.$data['SCPDURL']);
if ($xmlDesc === false)
{
echo "Not found:".$data['SCPDURL'].PHP_EOL;
continue;
}
$xmlDesc->registerXPathNamespace('fritzbox', $xmlDesc->getNameSpaces(false)[""]);
$xmlActionList = $xmlDesc->xpath("//fritzbox:action[fritzbox:argumentList/fritzbox:argument/fritzbox:direction ='out']");
$xmlStateVariable = $xmlDesc->xpath('//fritzbox:stateVariable');
foreach ($xmlStateVariable as $StateVariable)
{
$StateVariables[(string)$StateVariable->name] = (string)$StateVariable->dataType;
}
foreach ($xmlActionList as $ActionList)
{
foreach ($ActionList->argumentList->argument as $Argument)
{
if ((string)$Argument->direction == "in") continue 2;
$relatedStateVariable[(string)$Argument->name]=(string)$Argument->relatedStateVariable;
}
$client = new SoapClient(null,
array(
'location' => $data["location"],
'uri' => $data["uri"],
'noroot' => True,
'login' => $fb_user,
'password' => $fb_password,
'trace'=>TRUE,
'exceptions'=>false
)
);
$status = $client->{(string)$ActionList->name}();
if (is_soap_fault($status))
{
echo "--------------ERROR START----------------".PHP_EOL;
echo " descXML: ".$data['descXML'].PHP_EOL;
echo " SCPDURL: ".$data['SCPDURL'].PHP_EOL;
echo " Location: ".$data["location"].PHP_EOL;
echo " URI: ".$data["uri"].PHP_EOL;
echo " Action: ".(string)$ActionList->name.PHP_EOL;
echo "--------------ERROR END------------------".PHP_EOL.PHP_EOL;;
continue;
} else {
echo " descXML: ".$data['descXML'].PHP_EOL;
echo " SCPDURL: ".$data['SCPDURL'].PHP_EOL;
echo " Location: ".$data["location"].PHP_EOL;
echo " URI: ".$data["uri"].PHP_EOL;
echo " Action: ".(string)$ActionList->name.PHP_EOL;
print_r($status);
echo PHP_EOL.PHP_EOL;
}
$actionIdent=str_replace(array(".",":","-","_"),array("","","",""),(string)$ActionList->name);
if (!ctype_alnum($actionIdent))
{
echo "ERROR: Konnte Action nicht hinzufügen. Name kann nicht als Ident verwendet werden:".(string)$ActionList->name.PHP_EOL;
continue;
}
$action_id = @ IPS_GetObjectIDByIdent($actionIdent,$service_id);
if ($action_id===false)
{
$action_id = IPS_CreateInstance("{485D0419-BE97-4548-AA9C-C083EB82E61E}");
IPS_SetIdent($action_id, $actionIdent);
IPS_SetParent($action_id, $service_id);
IPS_SetName($action_id,(string)$ActionList->name);
}
if (count($ActionList->argumentList->argument) == 1)
{
UpdateIPSvar($action_id,(string)$ActionList->argumentList->argument->relatedStateVariable,$status,$StateVariables[(string)$ActionList->argumentList->argument->relatedStateVariable]);
}
else
{
foreach ($status as $key=>$value)
{
UpdateIPSvar($action_id,$relatedStateVariable[$key],$value,$StateVariables[$relatedStateVariable[$key]]);
}
}
}
}
function UpdateIPSvar($parent,$ident,$value,$type)
{
$ident=str_replace(array(".",":","-","_"),array("","","",""),$ident);
if (!ctype_alnum($ident))
{
echo "ERROR: Konnte Variable nicht hinzufügen. Name kann nicht als Ident verwendet werden:".$ident.PHP_EOL;
return;
}
switch ($type)
{
case "i1":
case "i2":
case "i4":
case "ui1":
case "ui2":
case "ui4":
$ips_type=1;
break;
case "boolean":
$ips_type=0;
break;
case "uuid":
case "dateTime":
case "string":
$ips_type=3;
break;
default:
echo "Unbekannt:".$type.PHP_EOL;
return;
break;
}
$var_id = @IPS_GetObjectIDByIdent($ident,$parent);
if ($var_id === false)
{
$var_id = IPS_CreateVariable($ips_type);
IPS_SetName($var_id,$ident);
IPS_SetIdent($var_id,$ident);
IPS_SetParent($var_id,$parent);
}
switch ($ips_type)
{
case 0:
if (GetValueBoolean($var_id) <> (bool)$value)
{
SetValueBoolean($var_id,(bool)$value);
}
break;
case 1:
if (GetValueInteger($var_id) <> (int)$value)
{
SetValueInteger($var_id,(int)$value);
}
break;
case 2:
if (GetValueFloat($var_id) <> round((float)$value,2))
{
SetValueFloat($var_id,round((float)$value,2));
}
break;
case 3:
if (GetValueString($var_id) <> $value)
{
SetValueString($var_id,$value);
}
break;
}
}
?>
Leider bringt er mir da folgende Fehlermeldung:
PHP Fatal error: Call to undefined function IPS_GetParent() in /var/www/scheduler/_module/API/fritzboxtest.php on line 41
Ich will einen raspberry pi dafür nutzen und habe Jessie drauf.
Kann mich wer in die richtige Richtung schubsen?
Muss dazu sagen das ich noch nicht viel mit PHP gemacht habe, ich benutze meist Python oder Perl für sowas.
Danke schon mal.
Gruß,
Gustl