Greetings

I'll start from afar. I recently wanted to set up an IPv6 connection via tunnelbroker, but what a bad luck - my ISP gives white dynamic IPv4 addresses, so I need to automate the process of updating the tunnel address. Tunnelbroker itself offers a solution, but it only works on its side, but not on mine, so every time you change the IPv4 address you have to delete the old tunnel and create a new one. Diligently, I found a solution, but I had problems with obtaining an IPv4 address and passing it as an argument to the netsh function. I contacted the author of the code, but he answered a couple of times and calmed down, but the problem did not remain solved. Here, in fact, is the link to the source: http://josherickson.org/132/hurricane-electric-6in4-windows-startup-script/

function fastpingtest { $ping = New-Object System.Net.NetworkInformation.Ping; $ping.Send("8.8.8.8", 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}"; $values = "USERID", "PASSWORDMD5HASH", TUNNELID; $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 

The problems begin after the comment. #Get connected interface The author advised to change ! $ .Contains ("Loopback"); on $ .contains ("Satel"); (because my internet connection is so called), I also found it logical to change findstr / c: "IP Address" to findstr / c: "IP Address" , since the original code was written under the English-language system, and I have Russian language . But still, the problem is the same - an incorrect address is written to the $ interface_ip variable (or not recorded at all), the wrong parameters are passed to the netsh command, and nothing works.

Googling the topic of extracting IPv4 addresses, found another option.

  #get connected interface $ipAddress = Test-Connection -ComputerName EAGauss -Count 1 | Select -ExpandProperty IPV4Address $interface_ip = $ipAddress.IPAddressToString 

Where EAGauss is the name of the host (my computer), I was delighted, because executing these two commands, everything works and my IPv4 address is displayed, but when I replace the old piece of code with this one, I still get the error:

-ERROR: Invalid IPv4 address supplied

What it means is that an invalid parameter is passed to the netsh function.

Please help. Sincerely, EAGauss

    2 answers 2

    Googling the topic of extracting IPv4 addresses, found another option.

     $ipAddress = Test-Connection -ComputerName EAGauss -Count 1 | Select -ExpandProperty IPV4Address $interface_ip = $ipAddress.IPAddressToString 

    The computer name can be taken from the environment, for example:

     $interface_ip = (Test-Connection -ComputerName ([System.Environment]::MachineName) -Count 1).IPV4Address.IPAddressToString 

    ... when I replace the old piece of code with this one, I still get the error:

    -ERROR: Invalid IPv4 address supplied

    What does it mean that an invalid parameter is passed to the netsh function

    I would first check if the netsh call from PowerShell works with hardcoded parameters. It is possible that arguments are incorrectly passed somewhere, while PowerShell calls to third-party executable files sometimes require special magic .

    Example, all IPv4 \ IPv6 addresses are invented, replace with your own:

     netsh interface teredo set state disabled netsh interface ipv6 add v6v4tunnel IP6Tunnel 1.2.3.4 4.3.2.1 netsh interface ipv6 add address IP6Tunnel 2001:0db8:0a0b:12f0:0000:0000:0000:0001 netsh interface ipv6 add route ::/0 IP6Tunnel 2001:0db8:0a0b:12f0:0000:0000:0000:0001 

      Finally, solved the problem - I will share the solution.
      The error -ERROR: Invalid IPv4 address supplied did not give out netsh, but the tunnelbroker site during authorization.
      Decided how:
      Reread Tunnelbroker.net API https://forums.he.net/index.php?topic=3153.0
      And corrected:

       function fastpingtest { $ping = New-Object System.Net.NetworkInformation.Ping; $ping.Send("8.8.8.8", 1000).status -eq "success"; } $endtime = [datetime]::Now.AddMinutes(1); $mapipv6 = $false; while([datetime]::Now -lt $endtime) { if(fastpingtest) { $mapipv6 = $true; break; } } if($mapipv6) { $ipAddress = Test-Connection -ComputerName hostname -Count 1 | Select -ExpandProperty IPV4Address $interface_ip = $ipAddress.IPAddressToString $wc = New-Object net.webclient; $url= "https://ipv4.tunnelbroker.net/ipv4_end.php?ip={0}&pass={1}&user_id={2}&tid={3}"; $values = $interface_ip, "MD5PASS", "USERID", TUNNELID; $wc.DownloadString(($url -f $values)); netsh interface ipv6 add address IP6Tunnel ClientIPv6Address netsh interface ipv6 add route ::/0 IP6Tunnel ServerIPv6Address netsh interface ipv6 delete interface IP6Tunnel netsh interface ipv6 add v6v4tunnel IP6Tunnel $interface_ip ServerIPv4Address } 

      Where hostname is the name of the local host; MD5PASS - Update Key (Personal account tunnelbroker, tunnel page, Advanced tab) USERID - User ID (Personal account tunnelbroker) TUNNELID - Tunnel ID (Personal account tunnelbroker, tunnel page)
      ClientIPv6Address, ServerIPv6Address, ServerIPv4Address - respectively (Personal account tunnelbroker, tunnel page).

      Now it gives the error: -ERROR: This tunnel is already associated with this IP address. Please try to limit your updates to IP changes. But this is normal, because my IPv4 address has not had time to change yet and 5 minutes have not passed since the previous request.