批处理文件负载均衡代码查询

时间:2014-02-11 11:22:34

标签: networking batch-file

我正在尝试从以下引用中运行bat文件代码:

Detecting batch IP conflict

然而,我收到一个错误: TRUE - 无效的别名动词 -1此时出人意料。

请有人向我解释这个问题。谢谢。下面附有示例代码

     @echo off
    setlocal

    :: Host to ping
    set primary=x.x.x.1
:: Ping with options (1 ping sent per loop, wait 500 ms for timeout)
set ping_options=-n 1 -w 500
:: Fail over after x ping failed responses
set fail_limit=5

:loop

    :: Ping x.x.x.1.  Test for "reply from".  If success, set failures=0; otherwise, increment failures
( ping %ping_options% %primary% 2>&1 | find /i "reply from" >NUL && set failures=0 ) || set /a "failures+=1"

:: If failures >= limit, switch IP
if failures GEQ %fail_limit% call :switch

:: Pause for a second and begin again.
ping -n 2 0.0.0.0 >NUL
goto loop


:: Switch subroutine
:switch

:: Get current IPv4 address
for /f "tokens=2 delims={}," %%I in ('wmic nicconfig where ipenabled="TRUE" get ipaddress /format:list') do set IP=%%~I

:: If the last character if the current IP is 1, switch to 2 (or vice versa)
if %IP:~-1%==1 ( set other=%IP:0,-1%2 ) else set other=%IP:0,-1%1

:: Perform the switch
netsh int ipv4 set address name="Local Area Connection" source=static address=%other% mask=255.255.255.0 gateway=none

1 个答案:

答案 0 :(得分:1)

您需要做的就是在wmic命令中转义等号。

for /f "tokens=2 delims={}," %%I in (
    'wmic nicconfig where ipenabled^="TRUE" get ipaddress /format:list'
) do set IP=%%~I