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

Windows bat file to set up tunnel and Routable prefix.

Started by ravenstar, March 05, 2014, 12:07:44 PM

Previous topic - Next topic

ravenstar

I think I had a senior citizen moment there  ;D

However I now have a script that should work.

It enumerates the devices on your network and if it comes up with more than one asks you to choose which to use - It's recommended you pick the LAN connection.

<#
    Pwershell Script to automate setting up a HE.net IPv6 Tunnel
    Copyright (C) 2014 Timothy Dutton
    Version 3
#>

# Change working directory to current script location
cd $PSScriptRoot
# Script needs administrator priviledges to run the Netsh coammnds correctly
# So check to see if we are already running as such
. .\Elevate.ps1
. .\GetTunnelInfo.ps1
Elevate

# Have already created a function GetTunnelInfo which prompts for a username and password
# And then connects to Tunnelbroker.net and gets the first tunnel found using their API
$ErrorActionPreference = "Stop"
$tunnel = Get-TunnelInfo

# Strip out the info we need to create the tunnel.  While not necessary it makes the
# netsh commands further down easier to read
$serverv4=$tunnel.serverv4
$clientv6=$tunnel.clientv6
$serverv6=$tunnel.serverv6
$routed64=$tunnel.routed64

# First Locate Which device is the Ethernet Adapter
$AdapterCount = 0;
$Choice = 0;
$AdapterList = Get-NetAdapter
$AdapterList | ForEach-Object { $AdapterCount = $AdapterCount + 1; Write-Host "$($AdapterCount)....$($_.Name)" }
if ($AdapterCount -eq 1)
{
    $Adapter = $AdapterList
}
else
{
    do
    {
        $Choice = Read-Host "Please enter number of adapter to be used (Note it is recommended that you use the Wired adapter)"
    } while ((1..$AdapterCount) -notcontains $Choice)
}
$Adapter = $AdapterList[($Choice-1)]
# Now Get the IPv4 address of the wired adapter
$ipAddress=Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $Adapter.ifIndex
$ipAddress=$ipAddress[0]
$clientv4=$ipAddress.IPAddress
# We also get the interface name and add quotes to the string so that if there are spaces
# The whole name is used in the netsh commands
$interface=$Adapter.Name
$interface='"'+$interface+'"'

# Finally do a simple replace to get the first IP address of the Routed /64
$firstAdress = $routed64.Replace("/64","1")

# Before setting up the Tunnel Display the Values
Write-Host "Client IPv4 Address is $($clientv4)"
Write-Host "Server IPv4 Address is $($serverv4)"
Write-Host "Client IPv6 Address is $($clientv6)"
Write-Host "Server IPv6 Address is $($serverv6)"
Write-Host "Routed /64 Prefix is   $($routed64)"
Write-Host "Network adapter used   $($interface)"
# Now we wait for user to press enter
Read-Host "Press Enter to set up tunnel or CTRL-C to abort"

# Now set up the basic tunnel
netsh int ipv6 add v6v4tunnel IP6Tunnel $clientv4 $serverv4
netsh int ipv6 add address IP6Tunnel $clientv6
netsh int ipv6 add route ::/0 IP6Tunnel $serverv6 publish=yes # Remove the publish=yes and everything below this line if you don't want to use your routed /64

# And enable forwarding on this and the LAN interface which will advertise the
# IPv6 Prefix
netsh int ipv6 set interface IP6Tunnel forwarding=enabled
netsh int ipv6 set interface $interface forwarding=enabled advertise=enabled

# Add the first address of the routed prefix to the LAN interface and publish the route
netsh int ipv6 add address $interface $firstAdress
# Sleep for 2 seconds else last command does not sleep successfully
sleep -s 2
netsh int ipv6 set route $routed64 $interface publish=yes


Follow the advice on the comment if you don't want to use the routed /64  however I would add that it shouldn't affect connections to IPv4 only sites anyway as any devices should only use the IPv6 tunnel if they are connecting to v6 addresses.

Ravenstar68

chrisjmartini

Quote from: ravenstar on March 21, 2014, 05:37:42 AM
I think I had a senior citizen moment there  ;D
However I now have a script that should work.
It enumerates the devices on your network and if it comes up with more than one asks you to choose which to use - It's recommended you pick the LAN connection.

That Worked! Props to the PowerShell guru!  ;D

ravenstar

Hardly a Guru.  In fact before this month I hadn't even touched powershell.  lol

Glad it finally works for you, if anything this has taught me that things that you'd expect to work can't always be relied on.  The Joys of Microsoft Windows I'm guessing lol.

Ravenstar68