Last active
July 29, 2025 12:15
-
-
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 hidden or 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 a new public IP: | |
| cls | |
| Remove-Variable * -ea 0 | |
| 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) | |
| $postSoap = { | |
| param($body, $action) | |
| $content = [System.Net.Http.StringContent]::new($body) | |
| $content.Headers.ContentType = 'text/xml' | |
| $content.Headers.Add('SoapAction', $action) | |
| $client.PostAsync($url, $content).Result | |
| } | |
| $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> | |
| ' | |
| foreach($version in 1..2) { | |
| $url = "http://fritz.box:49000/igd2upnp/control/WANIPConn$version" | |
| $result = &$postSoap $body 'urn:schemas-upnp-org:service:WANIPConnection:2#ForceTermination' | |
| if ($result.IsSuccessStatusCode) {break} | |
| } | |
| # 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> | |
| ' | |
| foreach ($sec in 1..30) { | |
| $result = &$postSoap $body 'urn:schemas-upnp-org:service:WANIPConnection:2#GetExternalIPAddress' | |
| $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" |
Author
good catch! I added that new logic into the initial code to support both versions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works fine. Nice :-)