Hurricane Electric's IPv6 Tunnel Broker Forums

General IPv6 Topics => IPv6 on Windows => Topic started by: LumKitty on October 10, 2014, 01:06:18 PM

Title: Batch file to update endpoint and create tunnel - no external dependencies
Post by: LumKitty on October 10, 2014, 01:06:18 PM
Apologies if this has been done before, but I've been working on a little script that will both create an IPv6 tunnel, and update the endpoint automatically.

Key features:
It needs to run as admin, so to run at startup do this (http://www.techrepublic.com/blog/windows-and-office/make-vista-launch-uac-restricted-programs-at-startup-with-task-scheduler/).

The aim is to be something you can just drop on the computer of a clueless friend or relative and forget about.

ToDo: Find a way to detect if native IPv6 becomes available and disable the tunnels, unfortunately I don't have native IPv6 available anywhere to test with.

Screenshot:
(http://i.imgur.com/U5ZXaVY.png)

@echo off

SET TunnelServer=IP of tunnel server
SET Tunnel=Put your /64 here
SET TunnelID=Found on the tunnel detailspage
SET TunnelKey=MD5 hashof your tunnel update key
SET UserID=Found on the main page below your real name

call:CheckIPv6||exit /b 0

if "%TunnelServer%"=="IP of tunnel server" (
    start notepad %0
    exit /b 1
)

set OldIPFile=%0\..\OldIP.txt
set UpdateResultFile=%temp%\output.txt
set EndPointUpdated=0

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
for /f "tokens=* delims= " %%a in ("%IP%") do set IP=%%a

for /f "delims=: tokens=2" %%a in ('nslookup myip.opendns.com resolver1.opendns.com 2^>nul ^| findstr "Address"') do (set ExternalIP=%%a)
for /f "tokens=* delims= " %%a in ("%ExternalIP%") do set ExternalIP=%%a

set OldIP=None
if exist %OldIPFile% for /f "tokens=*" %%c in (%OldIPFile%) do set OldIP=%%c

echo Current local IPv4 address     : %IP%
echo Current external IPv4 address  : %ExternalIP%
echo Previous external IPv4 address : %OldIP%

if not %OldIP%==%ExternalIP% (
    call:UpdateEndpoint
    call:CheckIPv6||exit /b 0
) else (
    echo Skipping Endpoint update       : [OK]
)

<nul set /p ".=Checking for admin rights      : "
net session >nul 2>&1
if %ErrorLevel%==0 (
    echo [OK]
    <nul set /p ".=Deleting any existing tunnels  : "
    netsh interface teredo set state disabled >nul
    netsh interface isatap set state disabled >nul
    netsh interface 6to4 set state disabled >nul
    netsh interface ipv6 delete interface IP6Tunnel >nul
    echo [OK]

    <nul set /p ".=Creating new tunnel            : [Interface] "
    netsh interface ipv6 add v6v4tunnel interface=IP6Tunnel %IP% %TunnelServer% >nul 2>nul
    if not %ErrorLevel%==0 (
        echo Failed to add interface!
    ) else (
        <nul set /p ".=[Address] "
        netsh interface ipv6 add address IP6Tunnel %Tunnel%::%IP% >nul 2>nul
        if not %ErrorLevel%==0 (
            echo [Error]
        ) else (
            <nul set /p ".=[Route] "
            netsh interface ipv6 add route ::/0 IP6Tunnel %Tunnel%::1 >nul 2>nul
            if not %ErrorLevel%==0 (
                echo [Error]
            ) else (
                echo [OK]
                call:CheckIPv6||exit /b 0
                call:UpdateEndpoint
                if not errorlevel 2 call:CheckIPv6||exit /b 0
                exit /b 1
            )
        )
    )
) else (
    echo [Error]
)

exit /b 1

:CheckIPv6
    <nul set /p ".=Checking IPv6 connectivity     : "
    ping -6 -n 1 ipv6.he.net | find "time=" >nul
    if not errorlevel 1 (
        echo [OK]
        exit /b 1
    ) else (
        echo [Failed]
        exit /b 0
    )

:UpdateEndpoint
    if %EndPointUpdated%==1 exit /b 2
    <nul set /p ".=Updating endpoint              :"
    bitsadmin /transfer "UpdateEndpoint" /priority foreground https://ipv4.tunnelbroker.net/ipv4_end.php?ip=%ExternalIP%^&pass=%TunnelKey%^&user_id=%UserID%^&tid=%TunnelID% %UpdateResultFile% > nul
    for /f "delims=: tokens=1" %%a in (%UpdateResultFile%) do set Result=%%a
    if not "%Result%"=="-OK" (
        set /p Result2=<"%UpdateResultFile%"
        if not "%Result2%"=="-ERROR: This tunnel is already associated with this IP address.  Please try to limit your updates to IP changes." (
            echo  [Error]
            echo %Result%
            exit /b 1
        )
    )
    echo  [OK]
    echo %ExternalIP%>%OldIPFile%
    del %temp%\output.txt >nul 2>nul
    set EndPointUpdated=1
    exit /b 0