1个用于切换到静态IP或DHCP的脚本

时间:2014-11-12 22:59:38

标签: batch-file static dhcp alternating

有没有办法让1个脚本检查当前的IP设置(DHCP或静态),然后交替使用另一个。

目前我有一个用于设置DHCP的脚本,另一个用于设置IP。我想要一个查看当前设置的脚本,然后切换到另一个。在一个完美的世界中,它会暂停告诉用户它正在切换的方向。

这些是我目前使用的。

脚本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

脚本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

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:2)

@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

此刻我无法测试它,但是......检查当前状态(代码搜索DHCP .... No)并切换到另一个

答案 1 :(得分:1)

真正的“一键”代码,不是暂停,而是超时: (MC ND代码的改进)

@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

答案 2 :(得分:0)

你可以使用这个有趣的技巧:

@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

非常重要最后一行(设置flipFlop = 0)不以新行(CR + LF)字符结尾。复制粘贴以前的代码,转到最后一个字符(Ctrl-End)并在必要时删除带有BackSpace的最后一个字符,这样最后一个字符就是&#34; 0&#34;在保存文件之前。

这个技巧有效,只有&#34;大约8180次;之后,您必须编辑文件并重置最后一行,因此它看起来像是原始文件。如果需要,可以插入额外的代码,以便程序自己测试这个条件并自动修复它。