Skip to content

Instantly share code, notes, and snippets.

@CarstenG2
Last active January 6, 2024 15:20
Show Gist options
  • Save CarstenG2/162ca553f9b5096499a1cc34fb88397b to your computer and use it in GitHub Desktop.
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 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()
@raffgear
Copy link

raffgear commented Jan 6, 2024

works fine. Nice :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment