检测批量IP冲突 [英] Detecting batch IP conflict

查看:100
本文介绍了检测批量IP冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何检测IP冲突?

How would you detect an IP conflict?

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

I am trying to implement a fail-over of two systems. Let us assume they take the IPs X.X.X.1 and X.X.X.2 (A and B for convenience), with A as the primary server and B as the back-up.

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

Both A and B will continuously ping X.X.X.1. Should A ever go down, B will detect "request timed out", and convert itself to X.X.X.1 using the following command:

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会收到:

When A reconnects itself, I want fail-over to happen smoothly and automatically. Now, since there are two machines with X.X.X.1, there will be an IP conflict. B retains X.X.X.1, while A, being the "later" computer, will receive this conflict. When A tries to ping X.X.X.1 again, A instead receives:

PING: General failure.

然后

A将以某种方式检测到这一点并将其自身转换为X.X.X.2.现在两台机器都运行良好,只是它们已镜像.

A will then detect this somehow and convert itself to X.X.X.2. Now both machines are running fine, just that they are mirror-imaged.

还是那是逻辑.目前,我在检测PING:常规故障时遇到问题.怎么会这样呢?

Or that's the logic anyway. Currently, I am having trouble detecting the PING: General failure. How would one go about doing this?

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

Or if there's a better way to do fail-over, what would it be?

推荐答案

我认为您需要先使用 2>& 1 ping 命令的stderr重定向到stdout.您可以测试错误.

I think you need to redirect the ping command's stderr to stdout with 2>&1 before you can test for errors.

: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

检查成功而不是失败可能会更好.如果.1死了,这应该从.2切换到.1;这要更坚固一些.并在发生冲突时从.1到.2.

It might be better to check for success rather than failure. Here's something a little more robust that should switch from .2 to .1 if .1 dies; and from .1 to .2 if conflict.

@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

这篇关于检测批量IP冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆