# get-fritzmactable.ps1
#
# (c) 2020 frank@carius.de
#
# Simple powershell to query the mac-table from a fritzbox via UPNP. FB does not support SNMP
# authentication required.
#
# 20201122 Initial Version
param (
[String]$hostURL = "https://192.168.178.1:49443", # IP of fritz-box
[string]$SOAPAction="urn:dslforum-org:service:Hosts:1#X_AVM-DE_GetHostListPath",
[Parameter(Mandatory=$true)][string]$FBKennwort="", # password for Fritz!Box
[switch]$onlyactive, #limit to active hosts
[switch]$prtg, #return PRTX XML wit num of active and passice hosts
[switch]$prtgdetail #return full xml with a channel per mac, be careful with many hosts
)
write-host "get-fritzmactable: Start"
if ($FBKennwort -eq "") {
Write-Host "Password required"
Write-Error "Password required"
exit
}
Write-host " Prepare SOAP-Request"
[string]$SOAPrequest = @"
"@
Write-host " Prepare Credentials"
$secure_pwd = $FBKennwort | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList "admin", $secure_pwd
Write-Host " Request DownloadURL via SOAP"
$ReturnXml = Invoke-RestMethod `
-Method POST `
-Headers @{'SOAPAction'=($soapaction)} `
-Uri ($hostURL+"/upnp/control/hosts") `
-Credential $creds `
-AllowUnencryptedAuthentication `
-ContentType 'text/xml' `
-SkipCertificateCheck `
-Body $SOAPrequest
Write-host " Download MAC-List from ($hostURL($($ReturnXml.Envelope.Body.'X_AVM-DE_GetHostListPathResponse'.'NewX_AVM-DE_HostListPath')))"
$devicehostlist = invoke-restmethod `
-Uri ($hostURL+($ReturnXml.Envelope.Body.'X_AVM-DE_GetHostListPathResponse'.'NewX_AVM-DE_HostListPath')) `
-SkipCertificateCheck
# convert System.Xml.XmlLinkedNode to standard Object
$mactable = $devicehostlist.List.Item.GetEnumerator() | ConvertTo-Csv | ConvertFrom-Csv
Write-host "Total Entries: $($mactable.count)"
Write-host " Loading List Done"
if ($prtgdetail) {
"
Active Hosts
$(($mactable | where-object {$_.active -eq 1}).count)
0
Passive Hosts
$(($mactable | where-object {$_.active -eq 0}).count)
0
Guest Hosts
$(($mactable | where-object {$_."X_AVM-DE_Guest" -eq 1}).count)
0
"
foreach ($mac in $mactable) {
"
$($mac.MACAddress)
$($mac.active)
0
"
}
"Anzahl der Eintraege in der FB MacTabelle
"
}
elseif ($prtg) {
"
Active Hosts
$(($mactable | where-object {$_.active -eq 1}).count)
0
Passive Hosts
$(($mactable | where-object {$_.active -eq 0}).count)
0
Guest Hosts
$(($mactable | where-object {$_."X_AVM-DE_Guest" -eq 1}).count)
0
Anzahl der Eintraege in der FB MacTabelle
"
}
else {
if ($onlyactive) {
Write-Host "Export active entries"
$mactable | where-object {$_.active -eq 1}
}
else {
Write-Host "Export all entries"
$mactable
}
}