使用keepalived设置vip的时候发现vip无法连接经查是出现了ip地址冲突使用了一个在用的ip作为了vip但是这个ip其实ping不通因为目标机禁用了ping也即是丢弃了ICMP包。一、那么怎么检测IP地址是否已经被占用呢1、ping如果ip未占用ping的返回如下ping 10.10.10.225PING 10.10.10.225 (10.10.10.225) 56(84) bytes of data.From 10.10.10.125 icmp_seq1 Destination Host UnreachableFrom 10.10.10.125 icmp_seq2 Destination Host UnreachableFrom 10.10.10.125 icmp_seq3 Destination Host UnreachableFrom 10.10.10.125 icmp_seq4 Destination Host Unreachable如果ip地址被占用但对方禁用了ping包ping 10.10.10.215PING 10.10.10.215 (10.10.10.215) 56(84) bytes of data.^C--- 10.10.10.215 ping statistics ---35 packets transmitted, 0 received, 100% packet loss, time 33998ms检测局域网内所有ip的脚本 find_unreachable_ip_in_lan.sh#!/bin/sh if [ $1 ! ]; then lan_prefix$1 else lan_prefix10.10.10 fi for ((i2; i255; i1)) do ip$lan_prefix.$i existsping -W 5 -c 2 $ip | grep -i Unreachable | wc -l if [ $exists ! 0 ]; then echo $ip unreachable else # echo $ip existed arp $ip | grep $ip fi done2、arping如果ip地址未被占用返回arping -I eth0 -f 10.10.10.225ARPING 10.10.10.225 from 10.10.10.125 wlp2s0^CSent 16 probes (16 broadcast(s))Received 0 response(s)如果ip地址被占用返回arping -I eth0 -f 10.10.10.215ARPING 10.10.10.215 from 10.10.10.125 eth0Unicast reply from 10.10.10.215 [80:A3:21:36:25:C0] 3.542msSent 1 probes (1 broadcast(s))Received 1 response(s)3、arp-scanReleases · royhills/arp-scan · GitHub可以扫描出局域网内所有的ip地址和对应的mac从中也可以查出局域网内重复的ip地址。arp-scan -I eth0 10.10.10.0/24Interface: eth0, type: EN10MB, MAC: 22:7d:57:e1:2f:6f, IPv4: 10.10.10.125Starting arp-scan 1.9.7 with 256 hosts (https://github.com/royhills/arp-scan)10.10.10.1 03:74:9c:d1:62:65 Ruijie Networks Co.,LTD10.10.10.2 44:0b:35:e0:e5:70 Xilinx10.10.10.3 86:a3:b6:2a:5d:cf (Unknown: locally administered)10.10.10.4 70:f3:f4:17:b5:34 (Unknown)10.10.10.5 e1:ca:57:66:74:7b Apple, Inc.10.10.10.6 b4:22:e2:6c:6a:5e Bull Group Co., Ltd二、手动添加新vipip addr add 10.10.10.225 dev eth0删除ip addr del 10.10.10.225/32 dev eth0三、本机arp缓存查看本机arp缓存arp -n | grep $ip清空本机arp缓存arp -d $ip用本机物理IP发广播刷新局域网VIP物理IP101VIP166arping -c 3 -I eth0 -s 10.10.10.101 -U 10.10.10.166