Thursday, January 23, 2014

windows ip switcher

Where I work, I support multiple offices.  Most of the offices do not have DHCP, they use static ip addresses (not my decision).  So here is a script I made for changing the ip address settings on a windows laptop, this does have to be ran with full admin privileges.  You will have to change parts of this script for your usage (where ever you see an ip address, and probably want to change the labels for the networks also).

echo ip-switch ver 20081119

set baseoffice=hq

rem now we figure out what subnet we're on
for /f "tokens=2 delims=:" %%i in ('ipconfig ^|find "IP Address"') do set ipaddr=%%i
for /f "tokens=2,3 delims=." %%o in ('echo %ipaddr%') do set subnet=%%o.%%p


rem based on the sub-net we determine what our location is
if /i "%subnet%" == "10.10" set loc=hq
if /i "%subnet%" == "10.11" set loc=sub
if /i "%subnet%" == "11.11" set loc=b1
if /i "%subnet%" == "11.12"  set loc=b2
if "%loc%" == "hq" set comploc=acompanyoffice
if "%loc%" == "substation" set comploc=acompanyoffice
if "%loc%" == "branch1" set comploc=acompanyoffice
if "%loc%" == "branch2" set comploc=acompanyoffice
if "%comploc%" == "acompanyoffice" goto comploc
set comploc=remote
:comploc


echo off

:complocmenu
cls
if "%comploc%" == "acompanyoffiice" echo this computer is currently setup for %comploc% (%loc%) use
if "%comploc%" == "remote" echo this computer is currently setup for %comploc% use
echo where is this laptop now (so it can communicate with the internet)?
echo 1) your base office (%baseoffice%)
echo 2) remote, home/other office
echo x) exit (leave everything as it is)
echo if another office enter the two-three letter abbreviation for it,
echo (hq,sub,b1,b2,etc...), and press enter, otherwise,
set /p comploc=press 1,2 or x and then enter:
rem if we entered a correct choice go to the next menu
if "%comploc%" == "1" goto methodmenu
if "%comploc%" == "2" goto methodmenu
if "%comploc%" == "x" echo no changes will be made
if /i "%comploc%" == "x" goto endprog
if /i "%comploc%" == "hq" goto otheroffice
if /i "%comploc%" == "sub" goto otheroffice
if /i "%comploc%" == "b1" goto otheroffice
if /i "%comploc%" == "b2" goto otheroffice


cls
rem if we entered a wrong chose clear screen of distraction
rem and ask for correct input
echo "you must press 1 or 2, or just press enter"
goto complocmenu

:otheroffice
set baseoffice=%comploc%
goto methodmenu

:methodmenu
echo is the laptop plugged in or using wireless?
echo 1) plugged in
echo 2) wireless
echo x) endprog
set /p netmethod=press 1 or 2 and then press enter

if "%netmethod%" == "1" set conmethod="Local Area Connection"
if "%netmethod%" == "2" set conmethod="Wireless Network Connection"
if "%netmethod%" == "1" goto methodselected
if "%netmethod%" == "2" goto methodselected
if "%netmethod%" == "x" goto endprog
echo "incorrect method selected"
goto methodmenu
:methodselected


if "%comploc%" == "1" goto staticip
if "%comploc%" == "2" goto dhcpip

:staticip
echo we will configure the computer for jcc office use
rem goto endprog
rem remove the goto endprog when we fix these settings
rem set static ip address (needs to be edited)

for /f "tokens=1 delims=-" %%I in ('echo %baseoffice%') do set loc=%%I
for /f "tokens=2 delims=-" %%C in ('hostname') do set compip=%%C
echo computer ip is %compip%

if /i "%baseoffice%" == "h1" set netaddr=10.10.10
if /i "%baseoffice%" == "sub" set netaddr=10.10.11
if /i "%baseoffice%" == "b1" set netaddr=10.11.11
if /i "%baseoffice%" == "b2" set netaddr=10.11.12




echo network address is %netaddr%

rem netsh interface ip set address "Local Area Connection" static %netaddr%.%compip% 255.255.255.0 %netaddr%.1 1
rem netsh interface ip set dns "Local Area Connection" static 10.91.1.21
rem netsh interface ip set wins "Local Area Connection" static 204.130.253.130
netsh interface ip set address %conmethod% static %netaddr%.%compip% 255.255.255.0 %netaddr%.1 1
netsh interface ip set dns %conmethod% static 10.91.1.21
netsh interface ip set wins %conmethod% static 10.254.2.130
rem start /wait sysdm.cpl

goto endprog


:dhcpip
rem some netsh commands to set dhcp on the interface
netsh interface ip set address name=%conmethod% source=dhcp
netsh interface ip set dns name=%conmethod% source=dhcp
netsh interface ip set wins name=%conmethod% source=dhcp
goto endprog

:endprog
hostname
for /f "tokens=2 delims=:" %%i in ('ipconfig ^|find "Default Gateway"') do set gateway=%%i
ipconfig |find "Address"
ping %gateway%
echo if you got back replys your good, if any of the requests timed out that's not good.
pause