Skip to content

Instantly share code, notes, and snippets.

@CarstenG2
Last active July 29, 2025 12:15
Show Gist options
  • Select an option

  • Save CarstenG2/162ca553f9b5096499a1cc34fb88397b to your computer and use it in GitHub Desktop.

Select an option

Save CarstenG2/162ca553f9b5096499a1cc34fb88397b to your computer and use it in GitHub Desktop.
Powershell-Code to force a reconnect on an AVM Fritz.Box
# 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"
@raffgear
Copy link

raffgear commented Jan 6, 2024

works fine. Nice :-)

@CarstenG2
Copy link
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