便民 发表于 2018-8-30 09:33:30

【shell】使用ping检测机器是否不在线

#!/bin/bash  
#set -x
  
#crontab
  
# 8 * * * * ......
  
function chkserdown()
  
{
  
ping -c 1 $1 > /dev/null
  
if [ $? -eq 0 ];then
  if [ -f $2 ];then
  rm -f $2
  icgname=`basename $2`"_is_ok"
  #echo $icgname" is ok"
  #这里发送邮件到139手机邮箱
  fi
  
else
  if [ ! -f $2 ];then
  sleep 5
  ping -c 1 $1 > /dev/null
  if [ $? -ne 0 ];then
  touch $2
  icgname=`basename $2`
  GW=`echo $1 | sed 's/\.\{1,3\}$/\.1/'`
  ping -c 1 $GW > /dev/null
  if [ $? -eq 0 ];then
  icgname=$icgname"_gw_isok_but_host_notok"
  else
  icgname=$icgname"_gw_and_host_notok"
  fi
  #这里发送邮件到139手机邮箱
  fi
  fi
  
fi
  
}
  
MSer="/tmp/MSerdown"
  
FileSer="/tmp/FileSerdown"
  
chkserdown "192.168.168.254" $MSer
  
chkserdown "10.1.1.254" $FileSer


页: [1]
查看完整版本: 【shell】使用ping检测机器是否不在线