1 script to switch to and from static IP or DHCP

12k views Asked by At

Is there a way to have 1 script that checks current IP settings (DHCP or Static), and then alternates to the other.

Currently I have one script to set DHCP, and another to set IP. I would like one script that looks at the current settings, and then switches to the other. In a perfect world it would have a pause telling the user which direction it is being switched.

These are what I am using so far.

Scrip 1

@echo Be sure Network Cable is unplugged and

@pause

netsh int ipv4 set address name="Local Area Connection" source=static address=10.38.xxx.xxx mask=255.255.255.xxx gateway=10.38.xxx.xx

netsh int ipv4 set dns name="Local Area Connection" source=static address=10.99.xx.xx register=primary validate=no

Script 2

netsh interface ipv4 set address name="Local Area Connection" source=dhcp

netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp

@echo It may take a few moments for changes to take effect. You may close this window or
@pause

Thank you very much for your help.

3

There are 3 answers

0
Aacini On

You may use this interesting trick:

@echo off
setlocal

call :getFlipFlop
if %flipFlop:~-1% equ 0 (
   echo Passing to ON state
   set /P "=1" <NUL >>"%~F0"
   rem Place here the commands for ON state...
) else (
   echo Passing to OFF state
   set /P "=0" <NUL >>"%~F0"
   rem Place here the commands for OFF state...
)
goto :EOF


rem Do NOT place anything below these lines
:getFlipFlop
set flipFlop=0

It is very important that the last line (set flipFlop=0) does NOT end in a new-line (CR+LF) characters. Copy-paste previous code, goto last character (Ctrl-End) and delete last characters with BackSpace if necessary so the last character is the "0" before save the file.

This trick works "only" for about 8180 times; after that, you must edit the file and reset the last line, so it looks like the original one again. If needed, additional code may be inserted so the program test by itself this condition and fix it automatically.

0
MC ND On
@echo off
    setlocal enableextensions disabledelayedexpansion

    set "name=Local Area Connection"

    echo Be sure Network Cable is unplugged and
    pause

    netsh interface ipv4 show addresses "%name%" | findstr /r /i /c:"DHCP.*No$" >nul 2>nul 

    if errorlevel 1 (

        echo DHCP mode - Press any key to change to static IP
        pause >nul 
        netsh int ipv4 set address name="%name%" source=static address=10.38.xxx.xxx  mask=255.255.255.xxx  gateway=10.38.xxx.xx
        netsh int ipv4 set dns     name="%name%" source=static address=10.99.xx.xx    register=primary      validate=no

    ) else (

        echo Static mode - Press any key to change to DHCP
        pause >nul 
        netsh interface ipv4 set address    name="%name%" source=dhcp
        netsh interface ipv4 set dnsservers name="%name%" source=dhcp
    )

    echo It may take a few moments for changes to take effect. You may close this window or
    pause

At this moment i can not test it, but... Check the current state (code searchs for DHCP .... No) and switch to the other

0
Rafael Lima On

Real "one click" code, not pauses but timeouts: (An improvement of MC ND code)

@echo off
setlocal enableextensions disabledelayedexpansion

rem #### ADAPTOR'S NAME  ####
set "name=Ethernet"

rem #### IP ####
set "ip=10.19.51.123"

rem #### GATEWAY ####
set "gw=10.19.51.14"

rem #### MASK ####
set "mask=255.255.255.0"

rem #### DNS ####
set "dns1=8.8.8.8"
set "dns2=8.8.4.4"


netsh interface ipv4 show addresses "%name%" | findstr /r /i /c:"ip.*%ip%$" >nul 2>nul 

if errorlevel 1 (

    echo ENTERING MANUAL IP MODE.
    ping 127.0.0.1 -n 2 > nul
    netsh int ipv4 set address name="%name%" source=static address=%ip%     mask=%mask%           gateway=%gw%
    netsh int ipv4 set dns     name="%name%" source=static address=%dns1%   register=primary      validate=no
    netsh interface ip add dns name="%name%"               address=%dns2%   index=2               validate=no

) else (

    echo ENTERING DHCP MODE.
    ping 127.0.0.1 -n 2 > nul
    netsh interface ipv4 set address    name="%name%" source=dhcp
    netsh interface ipv4 set dnsservers name="%name%" source=dhcp
)

echo CLOSING...
ping 127.0.0.1 -n 2 > nul