Getting IPv4 address and using it as an argument for netsh function

Welcome Forums General PowerShell Q&A Getting IPv4 address and using it as an argument for netsh function

This topic contains 2 replies, has 3 voices, and was last updated by

3 years, 8 months ago.

  • Author
  • #29134

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    Hi there!

    About a week ago I decided to set up the IPv6 connection via tunnelbroker but my ISP gives only dynamic dedicated IPv4 addresses. The tunnelbroker gives a solution to update the dynamic IPv4 address but it works only on server side. It means that every time my IPv4 address changes, I need to delete the old tunnel and create a new one.
    I've found a solution, the Powershsell script
    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= "{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

    But it didn't work – the problem starts from #get connected interface
    comment. I contacted the Author and he adviced me to replace !$.contains("Loopback"); with $.contains("Satel"); (name of my Internet connection) and also I decided to replace findstr /c:"IP Address" with findstr /c:"IP-Адрес" (because I have a russian version of Windows 7).
    Anyway the problem is that $interface_ip gets wring value and netsh command gives an error:
    -ERROR: Invalid IPv4 address supplied

    Then I've found another way to get the IPv4 address:
    #get connected interface
    $ipAddress = Test-Connection -ComputerName EAGauss -Count 1 | Select -ExpandProperty IPV4Address
    $interface_ip = $ipAddress.IPAddressToString

    Where EAGauss – is my hostname. It worked separately from the code but when I tried to change old strings to the new ones – it gave the same error.
    -ERROR: Invalid IPv4 address supplied

    I'm waiting for your help.

  • #29135

    Topics: 6
    Replies: 236
    Points: 0
    Rank: Member
  • #29140

    Topics: 6
    Replies: 650
    Points: 2
    Rank: Member

    Need to see what data you are getting in your variables. You say that $interface_ip has the wrong value, but what do you mean by wrong? Is it getting an IP just off the wrong interface?, is it blank?, does it have some other data there?

The topic ‘Getting IPv4 address and using it as an argument for netsh function’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort