检测批量IP冲突

时间:2013-03-18 13:50:27

标签: batch-file failover

您如何检测IP冲突?

我正在尝试实现两个系统的故障转移。我们假设它们采用IP X.X.X.1和X.X.X.2(为方便起见A和B),A作为主服务器,B作为备份。

A和B都将持续ping X.X.X.1。如果A发生故障,B将检测到“请求超时”,并使用以下命令将自身转换为X.X.X.1:

netsh int ipv4 set address name="Local Area Connection" source=static address=X.X.X.1 mask=255.255.255.0 gateway=none

当A重新连接时,我希望故障转移能够顺利自动地进行。现在,由于有两台机器使用X.X.X.1,因此会出现IP冲突。 B保留X.X.X.1,而作为“后期”计算机的A将收到此冲突。当A再次尝试ping X.X.X.1时,A会收到:

PING: General failure.

然后,A会以某种方式检测到它,并将自身转换为X.X.X.2。现在两台机器都运行正常,只是它们是镜像的。

或者那就是逻辑。目前,我无法检测到PING:一般故障。怎么会这样做?

或者,如果有更好的方法进行故障转移,它会是什么?

2 个答案:

答案 0 :(得分:1)

我认为您需要将ping命令的stderr重定向到带有2>&1的stdout,然后才能测试错误。

:loop
ping x.x.x.1 2>&1 | find /i "general failure" && (
        netsh int ipv4 set address name="Local Area Connection" source=static address=X.X.X.2 mask=255.255.255.0 gateway=none
)
goto loop

检查成功而不是失败可能更好。这里有一些更强大的东西应该从.2切换到.1如果.1死掉;如果发生冲突,则从.1到.2。

@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)

作为批处理行,也许是

ping ..whateveryou'dusetogeneratetheerror.. X.X.X.1 |find /i "General failure" >nul
if not errorlevel 1 netsh...(as above)