Shop

My New Shop
MTS Creations

Subscribe to My YouTube Channel

Friday, September 13, 2013

Laptop startup script for a Home Hosted Domain

I run a server at my house, setup with a Dynamic IP address.  I access this server all the time and I hated when I was home that my "hosts" file was not right and I would have to update it when I was at home and then change it when I was at work.
I'm sure there is an easier way to solve this problem but I couldn't figure it out.

To solve this problem I created a windows startup script that will change out my hosts file depending on my IP address (home versus work).

<code>
@echo off
:start
IPCONFIG |FIND "IPv4" > c:\TEMPIP.txt
FOR /F "tokens=2 delims=:" %%a in (c:\TEMPIP.txt) do (
    if defined IP (
        echo IP is defined %%a
        call :process %%a
    ) else (
        echo Setting IP to %%a
        set IP=%%a
        call :process %%a
    )
)

:process
del c:\TEMPIP.txt
set IP=%~1
echo Your Internal IP Address should be %IP%
IF not x%IP:10.0.3.=%==x%IP% (
    call :home
    GOTO :EOF
) ELSE (
    call :work
    GOTO :EOF
)
pause
GOTO :EOF;

:work
IF exist "C:\Windows\System32\drivers\etc\hosts_work" (
    XCOPY /Y "C:\Windows\System32\drivers\etc\_work" "C:\Windows\System32\drivers\etc\hosts"
    echo "You are at WORK %IP%"
    move "C:\Windows\System32\drivers\etc\hosts_work" "C:\Windows\System32\drivers\etc\hosts_home"
) ELSE (
    echo "Sorry, you're still at WORK!"
)
GOTO :EOF;

:home
IF exist "C:\Windows\System32\drivers\etc\hosts_home" (
    XCOPY /Y "C:\Windows\System32\drivers\etc\_home" "C:\Windows\System32\drivers\etc\hosts"
    echo "You are at HOME %IP%"
    move "C:\Windows\System32\drivers\etc\hosts_home" "C:\Windows\System32\drivers\etc\hosts_work"
) ELSE (
    echo "WOW! Did you stay home?"
)
GOTO :EOF;

:EOF
set /p recheck=Recheck Connection (y/n)?:
if /i %recheck%==y goto start
if /i %recheck%==n goto goaway

:goaway
net stop ssh_tunnel
net start ssh_tunnel
echo "See you next time!"
pause
exit
</code>

This requires you to have two files inside of your "C:\Windows\System32\drivers\etc\" called "_home" and "_work".

_home contains something like this:
<code>
## - HOME HOSTS FILE
10.0.3.10    www.domain.com domain.com
</code>

_work would contain something like this: (note the line is commented out. Also this would be if you had some other IP address you needed it set to if your nameserver isn't working.)
<code>
## - WORK HOSTS FILE
#123.123.123.123    www.domain.com domain.com
</code>

There is a third file that I use to decide if I need to copy the file over or not and it just changes every time (hosts_work to hosts_home and vice versa)

The last thing you will notice in the script under the :goaway section is  the following lines:
<code>
:goaway
net stop ssh_tunnel
net start ssh_tunnel
</code>

This will start and stop a ssh_tunnel that I have configured to use plink to forward specific local ports to the server so that I can stream my music to my laptop.  (all of this is due to a very strict VPN that is so strict that they block almost every port.
Post a Comment