检测IP地址冲突的shell脚本-check_server_ip_conflict.sh

check_server_ip_conflict.sh

使用arping获取对应IP地址的MAC地址,如果和预料的不一致则报警;

 

 

#!/bin/bash

epg_addr_01="00:50:56:B7:64:2A"
epg_addr_02="00:50:56:B7:44:43"
vod_addr_01="00:50:56:B7:28:AB"
vod_addr_02="00:50:56:B7:02:6C"

epg_ip_01="10.108.144.1"
epg_ip_02="10.108.144.2"
vod_ip_01="10.108.144.3"
vod_ip_02="10.108.144.4"
epg_vip="10.108.144.5"
vod_vip="10.108.144.6"

log_check="/var/log/sunlight/check_ip_conflict.log"
log_time=$(date "+%Y/%m/%d %H:%M:%S")
log_date=$(date "+%Y%m%d")

event_check=0

function send_email()
{
  echo " " >> $log_check
  ping_count=$(ping -W 3 -c 1 baidu.com | grep ttl | wc -l)
  if [ $ping_count -eq 1 ];then
    echo "[ info ] Begin to send mail......" >> $log_check
    echo "[ info ] `date "+%Y/%m/%d %H:%M:%S"` send mail content : $1" >> $log_check
    if [ -f /sunlight/python/slt_send_mail.py ];then
      python /sunlight/python/slt_send_mail.py "$1"
      if [ $? -eq 0 ];then
        echo "[ SUCCESS ] `date "+%Y/%m/%d %H:%M:%S"` send mail finished!" >> $log_check
      else
        echo " [ Error ] `date "+%Y/%m/%d %H:%M:%S"` send mail failed! " >> $log_check
      fi
    else
      echo " [ Error ] `date "+%Y/%m/%d %H:%M:%S"` /sunlight/python/slt_send_mail.py does not exist! " >> $log_check
    fi
  else
    echo " [ Error ] `date "+%Y/%m/%d %H:%M:%S"` can't connect to the Internet! " >> $log_check
  fi
}

function send_email_use_epg()
{
  echo " " >> $log_check
  ping_count=$(ssh -o StrictHostKeyChecking=no -i /usr/local/sunlight/sshkeys/init.pk -p2222 10.108.144.1 "ping -W 3 -c 1 baidu.com | grep ttl | wc -l")
  if [ $ping_count -eq 1 ];then
    echo "[ info ] Begin to send mail......" >> $log_check
    echo "[ info ] `date "+%Y/%m/%d %H:%M:%S"` send mail content : $1" >> $log_check
    ssh -o StrictHostKeyChecking=no -i /usr/local/sunlight/sshkeys/init.pk -p2222 10.108.144.1 "python /sunlight/python/slt_send_mail.py \"$1\""
    if [ $? -eq 0 ];then
      echo "[ SUCCESS ] `date "+%Y/%m/%d %H:%M:%S"` send mail finished!" >> $log_check
    else
      echo " [ Error ] `date "+%Y/%m/%d %H:%M:%S"` send mail failed! " >> $log_check
    fi
  else
    echo " [ Error ] `date "+%Y/%m/%d %H:%M:%S"` can't connect to the Internet! " >> $log_check
  fi
}

while true
do
  echo " " >> $log_check
  echo "-----------------------------------------------" >> $log_check

  check_epg_01_ip=$( ip addr | grep $epg_ip_01 | wc -l)
  if [ $check_epg_01_ip -eq 1 ];then
    peer_ip=$epg_ip_02
    peer_mac=$epg_addr_02
    send_warn="send_email"
  else
    peer_ip=$epg_ip_01
    peer_mac=$epg_addr_01
    send_warn="send_email_use_epg"
  fi

#master and slave node need to arping peer ip;
echo "[ INFO ] `date "+%Y/%m/%d %H:%M:%S"` target IP : ${peer_ip} correct Mac : ${peer_mac}" >> $log_check
arping -w 20 -c 10 -I eth0 ${peer_ip} | ( while read line
do
  if [ "${line:0:7}" == "Unicast" ];then
    grep_peer_mac=$(echo $line | grep -o ${peer_mac} | wc -l)
    if [ $grep_peer_mac -eq 0 ];then
      event_check=1
      error_mac=$(echo $line | grep -E -o "\[(.*)\]")
      echo "[ Error ] `date "+%Y/%m/%d %H:%M%S"` ${peer_ip} find conflict!!!! error mac: $error_mac " >> $log_check
      break
    else
      echo $line >> $log_check
    fi
  fi
done
if [ $event_check -eq 1 ];then
  event_string="<p><span style='color:#FF0000'>亚特兰蒂斯报警监控</span></p>"
  event_string="$event_string<hr/>"
  event_string="$event_string<p>报警内容: 出现IP地址冲突!</p>"
  event_string="$event_string<p>冲突IP:${peer_ip}</p>"
  event_string="$event_string<p>正确的Mac地址:${peer_mac}</p>"
  event_string="$event_string<p>检测到的Mac地址: $error_mac</p>"
  event_string="$event_string<hr/><p>请登录检查!</p>"
  eval "$send_warn \"$event_string\""
fi
)

check_vip=$( ip addr | grep $epg_vip | wc -l)
#use slave node to check all the other ip;
if [ $check_vip -ne 1 ];then
  #check vod_ip_01
  echo "[ INFO ] `date "+%Y/%m/%d %H:%M:%S"` target IP : ${vod_ip_01} correct Mac : ${vod_addr_01}" >> $log_check
  arping -w 20 -c 10 -I eth0 ${vod_ip_01} | ( while read line
  do
    if [ "${line:0:7}" == "Unicast" ];then
      grep_vod_01=$(echo $line | grep -o ${vod_addr_01} | wc -l)
      if [ $grep_vod_01 -eq 0 ];then
        event_check=1
        error_mac=$(echo $line | grep -E -o "\[(.*)\]")
        echo "[ Error ] `date "+%Y/%m/%d %H:%M%S"` ${vod_ip_01} find conflict!!!! error mac: $error_mac " >> $log_check
        break
      else
        echo $line >> $log_check
      fi
fi
done
if [ $event_check -eq 1 ];then
  event_string="<p><span style='color:#FF0000'>亚特兰蒂斯报警监控</span></p>"
  event_string="$event_string<hr/>"
  event_string="$event_string<p>报警内容: 出现IP地址冲突!</p>"
  event_string="$event_string<p>冲突IP:${vod_ip_01}</p>"
  event_string="$event_string<p>正确的Mac地址:${vod_addr_01}</p>"
  event_string="$event_string<p>检测到的Mac地址: $error_mac</p>"
  event_string="$event_string<hr/><p>请登录检查!</p>"
  eval "$send_warn \"$event_string\""
fi
)

#check vod_ip_02
echo "[ INFO ] `date "+%Y/%m/%d %H:%M:%S"` target IP : ${vod_ip_02} correct Mac : ${vod_addr_02}" >> $log_check
arping -w 20 -c 10 -I eth0 ${vod_ip_02} | ( while read line
do
  if [ "${line:0:7}" == "Unicast" ];then
    grep_vod_02=$(echo $line | grep -o ${vod_addr_02} | wc -l)
    if [ $grep_vod_02 -eq 0 ];then
      event_check=1
      error_mac=$(echo $line | grep -E -o "\[(.*)\]")
      echo "[ Error ] `date "+%Y/%m/%d %H:%M%S"` ${vod_ip_02} find conflict!!!! error mac: $error_mac " >> $log_check
      break
    else
      echo $line >> $log_check
    fi
  fi
done
if [ $event_check -eq 1 ];then
  event_string="<p><span style='color:#FF0000'>亚特兰蒂斯报警监控</span></p>"
  event_string="$event_string<hr/>"
  event_string="$event_string<p>报警内容: 出现IP地址冲突!</p>"
  event_string="$event_string<p>冲突IP:${vod_ip_02}</p>"
  event_string="$event_string<p>正确的Mac地址:${vod_addr_02}</p>"
  event_string="$event_string<p>检测到的Mac地址: $error_mac</p>"
  event_string="$event_string<hr/><p>请登录检查!</p>"
  eval "$send_warn \"$event_string\""
fi
)


#check epg_vip
echo "[ INFO ] `date "+%Y/%m/%d %H:%M:%S"` target IP : ${epg_vip} correct Mac : ${epg_addr_01} || ${epg_addr_02}" >> $log_check
arping -w 20 -c 10 -I eth0 ${epg_vip} | ( while read line
do
  if [ "${line:0:7}" == "Unicast" ];then
    grep_epg_master=$(echo $line | grep -o ${epg_addr_01} | wc -l)
    grep_epg_slave=$(echo $line | grep -o ${epg_addr_02} | wc -l)
    if [[ $grep_epg_master -eq 0 && $grep_epg_slave -eq 0 ]];then
      event_check=1
      error_mac=$(echo $line | grep -E -o "\[(.*)\]")
      echo "[ Error ] `date "+%Y/%m/%d %H:%M%S"` ${epg_vip} find conflict!!!! error mac: $error_mac " >> $log_check
      break
    else
      echo $line >> $log_check
    fi
  fi
done
if [ $event_check -eq 1 ];then
  event_string="<p><span style='color:#FF0000'>亚特兰蒂斯报警监控</span></p>"
  event_string="$event_string<hr/>"
  event_string="$event_string<p>报警内容: 出现IP地址冲突!</p>"
  event_string="$event_string<p>冲突IP:${epg_vip}</p>"
  event_string="$event_string<p>正确的Mac地址:${epg_addr_01} || ${epg_addr_02}</p>"
  event_string="$event_string<p>检测到的Mac地址: $error_mac</p>"
  event_string="$event_string<hr/><p>请登录检查!</p>"
  eval "$send_warn \"$event_string\""
fi
)


#check vod_vip
echo "[ INFO ] `date "+%Y/%m/%d %H:%M:%S"` target IP : ${vod_vip} correct Mac : ${vod_addr_01} || ${vod_addr_02}" >> $log_check
arping -w 20 -c 10 -I eth0 ${vod_vip} | ( while read line
do
if [ "${line:0:7}" == "Unicast" ];then
  grep_vod_master=$(echo $line | grep -o ${vod_addr_01} | wc -l)
  grep_vod_slave=$(echo $line | grep -o ${vod_addr_02} | wc -l)
  if [[ $grep_vod_master -eq 0 && $grep_vod_slave -eq 0 ]];then
    event_check=1
    error_mac=$(echo $line | grep -E -o "\[(.*)\]")
    echo "[ Error ] `date "+%Y/%m/%d %H:%M%S"` ${vod_vip} find conflict!!!! error mac: $error_mac " >> $log_check
    break
  else
    echo $line >> $log_check
  fi
fi
done
if [ $event_check -eq 1 ];then
  event_string="<p><span style='color:#FF0000'>亚特兰蒂斯报警监控</span></p>"
  event_string="$event_string<hr/>"
  event_string="$event_string<p>报警内容: 出现IP地址冲突!</p>"
  event_string="$event_string<p>冲突IP:${vod_vip}</p>"
  event_string="$event_string<p>正确的Mac地址:${vod_addr_01} || ${vod_addr_02}</p>"
  event_string="$event_string<p>检测到的Mac地址: $error_mac</p>"
  event_string="$event_string<hr/><p>请登录检查!</p>"
  eval "$send_warn \"$event_string\""
fi
)
fi
sleep 600
done

posted on 2018-01-02 14:01  天涯飞鸿  阅读(747)  评论(0编辑  收藏  举报

导航