1

有没有办法让 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

非常感谢您的帮助。

4

3 回答 3

3
@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)并切换到另一个

于 2014-11-13T12:04:50.850 回答
2

真正的“一键式”代码,不是暂停而是超时:(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
于 2018-01-19T14:33:54.590 回答
1

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

@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 删除最后一个字符,以便在保存文件之前最后一个字符是“0”。

这个技巧“只”工作了大约 8180 次;之后,您必须编辑文件并重置最后一行,使其再次看起来像原来的那样。如果需要,可以插入额外的代码,以便程序自行测试这个条件并自动修复它。

于 2014-11-13T01:12:08.790 回答