mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-24 02:48:17 +02:00
Improved list-fritzbox-devices.ps1
This commit is contained in:
parent
0b44bf15e1
commit
31e0dfe88f
@ -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-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-formatted.ps1, lists the current working directory formatted in columns
|
||||||
list-fritzbox-calls.ps1, lists the FRITZ!Box calls
|
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-installed-software.ps1, lists the installed software
|
||||||
list-logbook.ps1, lists the content of the logbook
|
list-logbook.ps1, lists the content of the logbook
|
||||||
list-unused-files.ps1, lists unused files in a directory tree
|
list-unused-files.ps1, lists unused files in a directory tree
|
||||||
|
|
@ -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-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-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-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-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-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
|
* [list-cmdlets.ps1](Scripts/list-cmdlets.ps1) - lists the PowerShell cmdlets
|
||||||
|
@ -6,23 +6,21 @@
|
|||||||
.NOTES Author: Markus Fleschutz / License: CC0
|
.NOTES Author: Markus Fleschutz / License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param([string]$USERNAME = "", [string]$PASSWORD = "")
|
#Requires -Version 3
|
||||||
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"
|
|
||||||
|
|
||||||
if ($PSVersionTable.PSVersion.Major -lt 3) {
|
param([string]$Username = "", [string]$Password = "")
|
||||||
write-host "ERROR: Minimum Powershell Version 3.0 is required!" -F Yellow
|
if ($Username -eq "") {
|
||||||
return
|
$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'
|
[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
|
[System.Xml.XmlNamespaceManager]$ns = new-Object System.Xml.XmlNamespaceManager $serviceinfo.NameTable
|
||||||
$ns.AddNamespace("ns",$serviceinfo.DocumentElement.NamespaceURI)
|
$ns.AddNamespace("ns",$serviceinfo.DocumentElement.NamespaceURI)
|
||||||
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
|
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
|
||||||
@ -36,7 +34,7 @@ function Execute-SOAPRequest { param([Xml]$SOAPRequest, [string]$soapactionheade
|
|||||||
$wr.Accept = 'text/xml'
|
$wr.Accept = 'text/xml'
|
||||||
$wr.Method = 'POST'
|
$wr.Method = 'POST'
|
||||||
$wr.PreAuthenticate = $true
|
$wr.PreAuthenticate = $true
|
||||||
$wr.Credentials = [System.Net.NetworkCredential]::new($USERNAME,$PASSWORD)
|
$wr.Credentials = [System.Net.NetworkCredential]::new($Username,$Password)
|
||||||
|
|
||||||
$requestStream = $wr.GetRequestStream()
|
$requestStream = $wr.GetRequestStream()
|
||||||
$SOAPRequest.Save($requestStream)
|
$SOAPRequest.Save($requestStream)
|
||||||
@ -79,19 +77,16 @@ function New-Request {
|
|||||||
$actiontag.AppendChild($el)| out-null
|
$actiontag.AppendChild($el)| out-null
|
||||||
}
|
}
|
||||||
$request.GetElementsByTagName('s:Body')[0].AppendChild($actiontag) | 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
|
return $resp
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:secport = (New-Request -urn "urn:dslforum-org:service:DeviceInfo:1" -action 'GetSecurityPort' -proto 'http').Envelope.Body.GetSecurityPortResponse.NewSecurityPort
|
$script:secport = (New-Request -urn "urn:dslforum-org:service:DeviceInfo:1" -action 'GetSecurityPort' -proto 'http').Envelope.Body.GetSecurityPortResponse.NewSecurityPort
|
||||||
|
|
||||||
function GetCallList(){
|
function GetCallList() { param([int]$MaxEntries = 999, [int]$MaxDays = 999
|
||||||
param(
|
|
||||||
[int]$maxentries = 999,
|
|
||||||
[int]$days = 999
|
|
||||||
)
|
)
|
||||||
$resp = New-Request -urn 'urn:dslforum-org:service:X_AVM-DE_OnTel:1' -action 'GetCallList'
|
$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
|
return $list.root.call
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,31 +1,23 @@
|
|||||||
#!/snap/bin/powershell
|
#!/snap/bin/powershell
|
||||||
<#
|
<#
|
||||||
.SYNTAX ./list-fritzbox-calls.ps1 [<username>] [<password>]
|
.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
|
.LINK https://github.com/fleschutz/PowerShell
|
||||||
.NOTES Author: Markus Fleschutz / License: CC0
|
.NOTES Author: Markus Fleschutz / License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param (
|
#Requires -Version 3
|
||||||
[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
|
|
||||||
)
|
|
||||||
|
|
||||||
write-host "get-fritzmactable: Start"
|
param([string]$Username = "", [string]$Password = "")
|
||||||
|
if ($Username -eq "") {
|
||||||
if ($FBKennwort -eq "") {
|
$Username = read-host "Enter username for FRITZ!Box"
|
||||||
Write-Host "Password required"
|
|
||||||
Write-Error "Password required"
|
|
||||||
exit
|
|
||||||
}
|
}
|
||||||
|
if ($Password -eq "") {
|
||||||
|
$Password = read-host "Enter password for FRITZ!Box"
|
||||||
Write-host " Prepare SOAP-Request"
|
}
|
||||||
|
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 = @"
|
[string]$SOAPrequest = @"
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
<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>
|
</s:Envelope>
|
||||||
"@
|
"@
|
||||||
|
|
||||||
Write-host " Prepare Credentials"
|
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
|
||||||
$secure_pwd = $FBKennwort | ConvertTo-SecureString -AsPlainText -Force
|
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
|
||||||
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $secure_pwd
|
|
||||||
|
|
||||||
Write-Host " Request DownloadURL via SOAP"
|
$XmlResult = invoke-restMethod `
|
||||||
$ReturnXml = Invoke-RestMethod `
|
|
||||||
-Method POST `
|
-Method POST `
|
||||||
-Headers @{'SOAPAction'=($soapaction)} `
|
-Headers @{'SOAPAction'=($SOAPAction)} `
|
||||||
-Uri ($hostURL+"/upnp/control/hosts") `
|
-Uri ($HostURL+"/upnp/control/hosts") `
|
||||||
-Credential $creds `
|
-Credential $Credentials `
|
||||||
-ContentType 'text/xml' `
|
-ContentType 'text/xml' `
|
||||||
-Body $SOAPrequest
|
-Body $SOAPrequest
|
||||||
|
|
||||||
Write-host " Download MAC-List from ($hostURL($($ReturnXml.Envelope.Body.'X_AVM-DE_GetHostListPathResponse'.'NewX_AVM-DE_HostListPath')))"
|
$HostList = invoke-restMethod -Uri ($HostURL+($XmlResult.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)"
|
|
||||||
|
|
||||||
Write-host " Loading List Done"
|
$HostTable = $HostList.List.Item.GetEnumerator()
|
||||||
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>"
|
|
||||||
}
|
|
||||||
|
|
||||||
"<text>Anzahl der Eintraege in der FB MacTabelle</text>
|
$HostTable | format-table -property Active,IPAddress,MACAddress,HostName,InterfaceType,X_AVM-DE_Speed
|
||||||
</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>"
|
|
||||||
|
|
||||||
}
|
exit 0
|
||||||
else {
|
|
||||||
if ($onlyactive) {
|
|
||||||
Write-Host "Export active entries"
|
|
||||||
$mactable | where-object {$_.active -eq 1}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Write-Host "Export all entries"
|
|
||||||
$mactable
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user