• Welcome to Hurricane Electric's IPv6 Tunnel Broker Forums.

Windows Powershell Dynamic IP update

Started by snoj, August 02, 2011, 01:09:48 PM

Previous topic - Next topic


HE's twitter asked me to share this with everyone...so here it is.

Original post: http://josherickson.org/132/hurricane-electric-6in4-windows-startup-script/

The code is pretty rudimentary and probably needs some sleeps in the while loop as well as some other things. But it does work and is great for those setups where you don't have a linux box around that's able to do the tunneling or you have a copy of Windows Server laying around.

function fastpingtest {
    $ping = New-Object System.Net.NetworkInformation.Ping;
    $ping.Send("", 1000).status -eq "success";
$endtime = [datetime]::Now.AddMinutes(1);
$mapipv6 = $false;
while([datetime]::Now -lt $endtime) {
    if(fastpingtest) { $mapipv6 = $true; break; }

if($mapipv6) {
    $wc = New-Object net.webclient;
    $url= "https://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&pass={1}&apikey={0}&tid={2}";

    $wc.DownloadString(($url -f $values));

    #get connected interface
    $interface = netsh interface ipv4 show interface | findstr /c:" connected" | ?{!$_.contains("Loopback");} | %{[regex]::Split($_, "( )+") | ?{$_.trim().length -gt 0} | %{$_.trim()}; }
    $interface_ip = (netsh interface ipv4 show address $interface[0] | findstr /c:"IP Address" | select -First 1).split(":")[1].trim()
    netsh interface teredo set state disabled
    netsh interface ipv6 add v6v4tunnel IP6Tunnel $interface_ip HEIPv4ENDPOINT
    netsh interface ipv6 add address IP6Tunnel YOURIPv6ADDRESS
    netsh interface ipv6 add route ::/0 IP6Tunnel HEIPv6ADDRESS

Originally, I was just going to use my HTPC to be the endpoint and ipv6 router. But as it was running Windows7 I had issues with firewalls. So now I'm running my tunnel through an old HP laptop I had lying around which takes care of all that before even routing traffic.

Enjoy! :D