Batch commands to change IP and DNS

Taranjeet Singh picture Taranjeet Singh · Oct 9, 2015 · Viewed 10k times · Source

I have written few commands in batch to execute them on choice basis. I am using 2 IP addresses for which I have to change IPv4 and DNS every time I switch between IPs.

I have done this code and this works correctly if I execute line by line but in batch they give errors.

@ECHO OFF 
SET /P no= Welcome dude so what are you up to press 1 for buzznet,2 for BSNL :

IF "%NO%"=="1" GOTO BUZZ
IF "%NO%"=="2" GOTO BSNL

:BUZZ
netsh interface ipv4 set address name="Ethernet" source=static ^
      addr=192.168.22.19 mask=255.255.255.0 gateway=192.168.22.1
netsh interface ip add dns name="Ethernet" addr=192.168.18.1
netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=2

:BSNL
netsh interface ip set address "Ethernet" dhcp
netsh interface ip set dns “Ethernet” dhcp
pause

enter image description here

Answer

Paul picture Paul · Oct 9, 2015

As stated in comments you need to add something that stop the script continuing when the job is done. (goto:EOF or exit /b 0)

@ECHO OFF 

:retry
SET /P no= Welcome dude so what are you up to press 1 for buzznet,2 for BSNL :

IF "%no%"=="1" GOTO BUZZ
IF "%no%"=="2" GOTO BSNL
rem if %no% is not 1 nor 2 then exit or goto :retry.
exit /b 0

:BUZZ
netsh interface ipv4 set address name="Ethernet" source=static ^
      addr=192.168.22.19 mask=255.255.255.0 gateway=192.168.22.1
netsh interface ip add dns name="Ethernet" addr=192.168.18.1
netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=2
rem job done, then exit with a pause before
pause
exit /b 0

:BSNL
netsh interface ip set address "Ethernet" dhcp
netsh interface ip set dns "Ethernet" dhcp
pause

Also the last command are malformed with quotes “Ethernet” should be "Ethernet"