Improved list-fritzbox-devices.ps1

This commit is contained in:
Markus Fleschutz 2021-01-03 17:27:38 +01:00
parent 0b44bf15e1
commit 31e0dfe88f
4 changed files with 37 additions and 114 deletions

View File

@ -23,7 +23,7 @@ list-empty-dirs.ps1, lists empty subfolders in a directory tree
list-files.ps1, lists all files in the given folder and also in every subfolder
list-formatted.ps1, lists the current working directory formatted in columns
list-fritzbox-calls.ps1, lists the FRITZ!Box calls
list-fritzbox-devices.ps1, lists the FRITZ!Box devices
list-fritzbox-devices.ps1, lists FRITZ!Box's known devices
list-installed-software.ps1, lists the installed software
list-logbook.ps1, lists the content of the logbook
list-unused-files.ps1, lists unused files in a directory tree

1 Filename Description
23 list-files.ps1 lists all files in the given folder and also in every subfolder
24 list-formatted.ps1 lists the current working directory formatted in columns
25 list-fritzbox-calls.ps1 lists the FRITZ!Box calls
26 list-fritzbox-devices.ps1 lists the FRITZ!Box devices lists FRITZ!Box's known devices
27 list-installed-software.ps1 lists the installed software
28 list-logbook.ps1 lists the content of the logbook
29 list-unused-files.ps1 lists unused files in a directory tree

View File

@ -32,7 +32,7 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol
* [list-files.ps1](Scripts/list-files.ps1) - lists all files in the given folder and also in every subfolder
* [list-formatted.ps1](Scripts/list-formatted.ps1) - lists the current working directory formatted in columns
* [list-fritzbox-calls.ps1](Scripts/list-fritzbox-calls.ps1) - lists the FRITZ!Box calls
* [list-fritzbox-devices.ps1](Scripts/list-fritzbox-devices.ps1) - lists the FRITZ!Box devices
* [list-fritzbox-devices.ps1](Scripts/list-fritzbox-devices.ps1) - lists FRITZ!Box's known devices
* [list-logbook.ps1](Scripts/list-logbook.ps1) - lists the content of the logbook
* [list-unused-files.ps1](Scripts/list-unused-files.ps1) - lists unused files in a directory tree
* [list-cmdlets.ps1](Scripts/list-cmdlets.ps1) - lists the PowerShell cmdlets

View File

@ -6,23 +6,21 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
param([string]$USERNAME = "", [string]$PASSWORD = "")
if ($USERNAME -eq "") {
$USERNAME = read-host "Enter username for FRITZ!Box"
}
if ($PASSWORD -eq "") {
$PASSWORD = read-host "Enter password for FRITZ!Box"
}
$FB_FQDN = "fritz.box"
#Requires -Version 3
if ($PSVersionTable.PSVersion.Major -lt 3) {
write-host "ERROR: Minimum Powershell Version 3.0 is required!" -F Yellow
return
param([string]$Username = "", [string]$Password = "")
if ($Username -eq "") {
$Username = read-host "Enter username for FRITZ!Box"
}
if ($Password -eq "") {
$Password = read-host "Enter password for FRITZ!Box"
}
write-progress "Contacting FRITZ!Box ..."
$FQDN = "fritz.box"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
[xml]$serviceinfo = Invoke-RestMethod -Method GET -Uri "http://$($FB_FQDN):49000/tr64desc.xml"
[xml]$serviceinfo = Invoke-RestMethod -Method GET -Uri "http://$($FQDN):49000/tr64desc.xml"
[System.Xml.XmlNamespaceManager]$ns = new-Object System.Xml.XmlNamespaceManager $serviceinfo.NameTable
$ns.AddNamespace("ns",$serviceinfo.DocumentElement.NamespaceURI)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
@ -36,7 +34,7 @@ function Execute-SOAPRequest { param([Xml]$SOAPRequest, [string]$soapactionheade
$wr.Accept = 'text/xml'
$wr.Method = 'POST'
$wr.PreAuthenticate = $true
$wr.Credentials = [System.Net.NetworkCredential]::new($USERNAME,$PASSWORD)
$wr.Credentials = [System.Net.NetworkCredential]::new($Username,$Password)
$requestStream = $wr.GetRequestStream()
$SOAPRequest.Save($requestStream)
@ -79,19 +77,16 @@ function New-Request {
$actiontag.AppendChild($el)| out-null
}
$request.GetElementsByTagName('s:Body')[0].AppendChild($actiontag) | out-null
$resp = Execute-SOAPRequest $request "$($service.serviceType)#$($action)" "$($Protocol)://$($FB_FQDN):$(@{$true=$script:secport;$false=49000}[($Protocol -eq 'https')])$($service.controlURL)"
$resp = Execute-SOAPRequest $request "$($service.serviceType)#$($action)" "$($Protocol)://$($FQDN):$(@{$true=$script:secport;$false=49000}[($Protocol -eq 'https')])$($service.controlURL)"
return $resp
}
$script:secport = (New-Request -urn "urn:dslforum-org:service:DeviceInfo:1" -action 'GetSecurityPort' -proto 'http').Envelope.Body.GetSecurityPortResponse.NewSecurityPort
function GetCallList(){
param(
[int]$maxentries = 999,
[int]$days = 999
function GetCallList() { param([int]$MaxEntries = 999, [int]$MaxDays = 999
)
$resp = New-Request -urn 'urn:dslforum-org:service:X_AVM-DE_OnTel:1' -action 'GetCallList'
$list = [xml](new-object System.Net.WebClient).DownloadString("$($resp.Envelope.Body.GetCallListResponse.NewCallListURL)&max=$maxentries&days=$days")
$list = [xml](new-object System.Net.WebClient).DownloadString("$($resp.Envelope.Body.GetCallListResponse.NewCallListURL)&max=$MaxEntries&MaxDays=$days")
return $list.root.call
}

View File

@ -1,31 +1,23 @@
#!/snap/bin/powershell
<#
.SYNTAX ./list-fritzbox-calls.ps1 [<username>] [<password>]
.DESCRIPTION lists the phone calls of the FRITZ!Box device
.DESCRIPTION lists FRITZ!Box's known devices
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param (
[String]$hostURL = "https://fritz.box:49443", # IP of fritz-box
[string]$SOAPAction="urn:dslforum-org:service:Hosts:1#X_AVM-DE_GetHostListPath",
[Parameter(Mandatory=$true)][string]$Username = "", # username for Fritz!Box
[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
)
#Requires -Version 3
write-host "get-fritzmactable: Start"
if ($FBKennwort -eq "") {
Write-Host "Password required"
Write-Error "Password required"
exit
param([string]$Username = "", [string]$Password = "")
if ($Username -eq "") {
$Username = read-host "Enter username for FRITZ!Box"
}
Write-host " Prepare SOAP-Request"
if ($Password -eq "") {
$Password = read-host "Enter password for FRITZ!Box"
}
write-progress "Contacting FRITZ!Box ..."
[string]$HostURL = "https://fritz.box:49443"
[string]$SOAPAction="urn:dslforum-org:service:Hosts:1#X_AVM-DE_GetHostListPath"
[string]$SOAPrequest = @"
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
@ -35,85 +27,21 @@ Write-host " Prepare SOAP-Request"
</s:Envelope>
"@
Write-host " Prepare Credentials"
$secure_pwd = $FBKennwort | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $secure_pwd
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
Write-Host " Request DownloadURL via SOAP"
$ReturnXml = Invoke-RestMethod `
$XmlResult = invoke-restMethod `
-Method POST `
-Headers @{'SOAPAction'=($soapaction)} `
-Uri ($hostURL+"/upnp/control/hosts") `
-Credential $creds `
-Headers @{'SOAPAction'=($SOAPAction)} `
-Uri ($HostURL+"/upnp/control/hosts") `
-Credential $Credentials `
-ContentType 'text/xml' `
-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'))
# convert System.Xml.XmlLinkedNode to standard Object
$mactable = $devicehostlist.List.Item.GetEnumerator() | ConvertTo-Csv | ConvertFrom-Csv
Write-host "Total Entries: $($mactable.count)"
$HostList = invoke-restMethod -Uri ($HostURL+($XmlResult.Envelope.Body.'X_AVM-DE_GetHostListPathResponse'.'NewX_AVM-DE_HostListPath'))
Write-host " Loading List Done"
if ($prtgdetail) {
"<?xml version=""1.0"" encoding=""UTF-8"" ?>
<prtg>
<result>
<channel>Active Hosts</channel>
<value>$(($mactable | where-object {$_.active -eq 1}).count)</value>
<float>0</float>
</result>
<result>
<channel>Passive Hosts</channel>
<value>$(($mactable | where-object {$_.active -eq 0}).count)</value>
<float>0</float>
</result>
<result>
<channel>Guest Hosts</channel>
<value>$(($mactable | where-object {$_."X_AVM-DE_Guest" -eq 1}).count)</value>
<float>0</float>
</result>"
foreach ($mac in $mactable) {
"<result>
<channel>$($mac.MACAddress)</channel>
<value>$($mac.active)</value>
<float>0</float>
</result>"
}
$HostTable = $HostList.List.Item.GetEnumerator()
"<text>Anzahl der Eintraege in der FB MacTabelle</text>
</prtg>"
}
elseif ($prtg) {
"<?xml version=""1.0"" encoding=""UTF-8"" ?>
<prtg>
<result>
<channel>Active Hosts</channel>
<value>$(($mactable | where-object {$_.active -eq 1}).count)</value>
<float>0</float>
</result>
<result>
<channel>Passive Hosts</channel>
<value>$(($mactable | where-object {$_.active -eq 0}).count)</value>
<float>0</float>
</result>
<result>
<channel>Guest Hosts</channel>
<value>$(($mactable | where-object {$_."X_AVM-DE_Guest" -eq 1}).count)</value>
<float>0</float>
</result>
<text>Anzahl der Eintraege in der FB MacTabelle</text>
</prtg>"
$HostTable | format-table -property Active,IPAddress,MACAddress,HostName,InterfaceType,X_AVM-DE_Speed
}
else {
if ($onlyactive) {
Write-Host "Export active entries"
$mactable | where-object {$_.active -eq 1}
}
else {
Write-Host "Export all entries"
$mactable
}
}
exit 0