3

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

Paul
  • 2,620
  • 2
  • 17
  • 27
Taranjeet Singh
  • 35
  • 1
  • 1
  • 5

2 Answers2

3

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"

Paul
  • 2,620
  • 2
  • 17
  • 27
1

I checked this before doing a script to change between a static IP and dynamic so I thought of adding my solution in case it´s useful.

  • There are two options also to check the status of the network interfaces and to check the current IP.

  • Everything for the interface called "Ethernet".

  • Because this needs to run as admin, what I did was this: this bat was stored in my folder, C:/workset/scripts/ then I created a shortcut, copied this on the Desktop. Then the shortcut, I went to Properties and there I checked the option to Run as admin and also just becase, changed the icon to a cool one. So now I can run it from the desktop as any other program and it runs as admin from the start.

     @ECHO OFF
      cls
      ECHO ________________________________
      ECHO    Only works if run as admin.
      ECHO.
      :MENU
      ECHO.
      ECHO.
      ECHO _______________________________
      ECHO                     MENU
      ECHO ................................
      ECHO Choose an option:
      ECHO.
      ECHO     1 IP static 192.X.X.X, 255.X,X.X
      ECHO     2 IP dynamic
      ECHO     3 Check current IP
      ECHO     4 Check network interfaces status
      ECHO     5 EXIT
      ECHO .................................. 
      ECHO.
      ECHO.
    
      SET /P M=Write 1, 2, 3, 4 or 5 and press ENTER:  
      IF %M%==1 GOTO STATIC
      IF %M%==2 GOTO DYNAMIC
      IF %M%==3 GOTO CHECK
      IF %M%==4 GOTO CHECKIF
      IF %M%==5 GOTO EOF
    
      :STATIC
      ECHO ______________________________
      ECHO.
      ECHO Changing IP to 192.X.X.X and subred mask to 255.X.X.X
      netsh interface ip set address name= "Ethernet" static 192.X.X.X 255.X.X.X
      TIMEOUT /T 2 /NOBREAK > NUL
      ECHO ____________________________
      netsh interface ip show config name="Ethernet"
      TIMEOUT /T 2 /NOBREAK > NUL
      GOTO MENU
    
    
      :DYNAMIC
      ECHO _______________________________
      ECHO.
      ECHO Changing to a dynamic IP...
      netsh interface ip set address "Ethernet" dhcp
      TIMEOUT /T 4 /NOBREAK > NUL
      ECHO _____________________________
      netsh interface ip show config name="Ethernet"
      TIMEOUT /T 2 /NOBREAK > NUL
      GOTO MENU
    
      :CHECK
      ECHO __________________________________
      ECHO Showing the adapter details...
      netsh interface ip show config name="Ethernet"
      TIMEOUT /T 2 /NOBREAK > NUL
      GOTO MENU
    
      :CHECKIF
      ECHO ____________________________________
      ECHO Showing the network interface status....
      netsh interface ipv4 show interfaces
      TIMEOUT /T 2 /NOBREAK > NUL
      GOTO MENU
    

There should be an error handling but I dont have time, so this is it.

Another note: tbh, at the end I just removed all the options and created one to change to the static IP and another to the dynamic IP so now I just run them faster.

testfailed
  • 31
  • 2
  • 5
  • Lol bro this question was ages ago. Thanks for help but i don't need it any more – Taranjeet Singh Oct 01 '20 at 21:42
  • 1
    My reason to add it was that if in the future I´m searching for this again I can find it, or someone else looking for the info. A lot of times I´m looking for something and find solutions in questions made a long time ago so I find very useful when people post their solutions at any time :) – testfailed Oct 05 '20 at 10:39