PowerShell/Scripts/list-fritzbox-calls.ps1

103 lines
4.0 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-09-24 17:19:49 +02:00
Lists the phone calls of the FRITZ!Box device
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script lists the phone calls of the FRITZ!Box device.
2021-10-15 23:09:08 +02:00
.PARAMETER Username
Specifies the user name for FRITZ!Box
.PARAMETER Password
Specifies the password to FRITZ!Box
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./list-fritzbox-calls.ps1
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-01-03 11:49:06 +01:00
#>
2021-09-11 11:37:22 +02:00
#Requires -Version 3
2021-07-15 15:51:22 +02:00
param([string]$Username = "", [string]$Password = "")
2021-04-21 19:53:52 +02:00
if ($Username -eq "") { $Username = read-host "Enter username for FRITZ!Box" }
if ($Password -eq "") { $Password = read-host "Enter password for FRITZ!Box" }
2021-02-18 20:17:55 +01:00
2021-01-03 17:27:38 +01:00
write-progress "Contacting FRITZ!Box ..."
$FQDN = "fritz.box"
2021-01-03 11:49:06 +01:00
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
2021-01-03 17:27:38 +01:00
[xml]$serviceinfo = Invoke-RestMethod -Method GET -Uri "http://$($FQDN):49000/tr64desc.xml"
2021-01-03 11:49:06 +01:00
[System.Xml.XmlNamespaceManager]$ns = new-Object System.Xml.XmlNamespaceManager $serviceinfo.NameTable
$ns.AddNamespace("ns",$serviceinfo.DocumentElement.NamespaceURI)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
function Execute-SOAPRequest { param([Xml]$SOAPRequest, [string]$soapactionheader, [String]$URL)
try {
$wr = [System.Net.WebRequest]::Create($URL)
$wr.Headers.Add('SOAPAction',$soapactionheader)
$wr.ContentType = 'text/xml; charset="utf-8"'
$wr.Accept = 'text/xml'
$wr.Method = 'POST'
$wr.PreAuthenticate = $true
2021-01-03 17:27:38 +01:00
$wr.Credentials = [System.Net.NetworkCredential]::new($Username,$Password)
2021-01-03 11:49:06 +01:00
$requestStream = $wr.GetRequestStream()
$SOAPRequest.Save($requestStream)
$requestStream.Close()
[System.Net.HttpWebResponse]$wresp = $wr.GetResponse()
$responseStream = $wresp.GetResponseStream()
$responseXML = [Xml]([System.IO.StreamReader]($responseStream)).ReadToEnd()
$responseStream.Close()
return $responseXML
} catch {
if ($_.Exception.InnerException.Response){
throw ([System.IO.StreamReader]($_.Exception.InnerException.Response.GetResponseStream())).ReadToEnd()
} else {
throw $_.Exception.InnerException
}
}
}
function New-Request {
param(
[parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$urn,
[parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$action,
[hashtable]$parameter = @{},
$Protocol = 'https'
)
# SOAP Request Body Template
[xml]$request = @"
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
</s:Body>
</s:Envelope>
"@
$service = $serviceinfo.SelectNodes('//ns:service',$ns) | ?{$_.ServiceType -eq $URN}
if(!$service){throw "URN does not exist."}
$actiontag = $request.CreateElement('u',$action,$service.serviceType)
$parameter.GetEnumerator() | %{
$el = $request.CreateElement($_.Key)
$el.InnerText = $_.Value
$actiontag.AppendChild($el)| out-null
}
$request.GetElementsByTagName('s:Body')[0].AppendChild($actiontag) | out-null
2021-01-03 17:27:38 +01:00
$resp = Execute-SOAPRequest $request "$($service.serviceType)#$($action)" "$($Protocol)://$($FQDN):$(@{$true=$script:secport;$false=49000}[($Protocol -eq 'https')])$($service.controlURL)"
2021-01-03 11:49:06 +01:00
return $resp
}
$script:secport = (New-Request -urn "urn:dslforum-org:service:DeviceInfo:1" -action 'GetSecurityPort' -proto 'http').Envelope.Body.GetSecurityPortResponse.NewSecurityPort
2021-01-27 15:12:17 +01:00
function GetCallList { param([int]$MaxEntries = 999, [int]$MaxDays = 999
2021-01-03 11:49:06 +01:00
)
$resp = New-Request -urn 'urn:dslforum-org:service:X_AVM-DE_OnTel:1' -action 'GetCallList'
2021-01-03 17:27:38 +01:00
$list = [xml](new-object System.Net.WebClient).DownloadString("$($resp.Envelope.Body.GetCallListResponse.NewCallListURL)&max=$MaxEntries&MaxDays=$days")
2021-01-03 11:49:06 +01:00
return $list.root.call
}
GetCallList | format-table -property Date,Duration,Caller,Called
echo $Result
2021-09-27 10:09:45 +02:00
exit 0 # success