Last active
January 6, 2024 15:20
-
-
Save CarstenG2/162ca553f9b5096499a1cc34fb88397b to your computer and use it in GitHub Desktop.
Powershell-Code to force a reconnect on an AVM Fritz.Box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# reset Internet-Connection on fritz.box to get new IP: | |
cls | |
Add-Type -AssemblyName System.Net.Http | |
$handler = [System.Net.Http.HttpClientHandler]::new() | |
$ignoreCerts = [System.Net.Http.HttpClientHandler]::DangerousAcceptAnyServerCertificateValidator | |
$handler.ServerCertificateCustomValidationCallback = $ignoreCerts | |
$client = [System.Net.Http.HttpClient]::new($handler) | |
$url = 'http://fritz.box:49000/igd2upnp/control/WANIPConn1' | |
$body = @" | |
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> | |
<s:Body> | |
<u:ForceTerminationResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:2" /> | |
</s:Body> | |
</s:Envelope> | |
"@ | |
$content = [System.Net.Http.StringContent]::new($body) | |
$content.Headers.ContentType = 'text/xml' | |
$content.Headers.Add('SoapAction', 'urn:schemas-upnp-org:service:WANIPConnection:2#ForceTermination') | |
$result = $client.PostAsync($url, $content).Result | |
# wait for the new IP: | |
$body = @" | |
<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> | |
<s:Body> | |
<u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:2' /> | |
</s:Body> | |
</s:Envelope>" | |
"@ | |
$oldIp = $null | |
foreach ($sec in 1..30) { | |
$content = [System.Net.Http.StringContent]::new($body) | |
$content.Headers.ContentType = 'text/xml' | |
$content.Headers.Add('SoapAction', 'urn:schemas-upnp-org:service:WANIPConnection:2#GetExternalIPAddress') | |
$result = $client.PostAsync($url, $content).Result | |
$response = $result.Content.ReadAsStreamAsync().Result | |
$stream = [System.IO.StreamReader]::new($response) | |
[xml]$data = $stream.ReadToEndAsync().Result | |
$ip = $data.Envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress | |
if ($oldIp -eq $null) {$oldIp = $ip; write-host "old-IP: $oldIp"} | |
if ($ip -and $ip -ne $oldIp) {break} | |
sleep 1 | |
} | |
write-host "new-IP: $ip" | |
$client.dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works fine. Nice :-)