实时ping检查服务器
1.实时ping命令检查需要实时记录返回检查时间,返回结果
date +"%F %T" ; ping -c 1 ${lv_ip_ping}|grep -i from 2>&1
2.检查后立即将结果输入到文件中
while true
do
lv_date=`date +"%F"`
echo `date +"%F %T" ; ping -c 1 127.0.0.1 |grep -i from 2>&1` >> ping_test.txt
sleep 1
done
3.建立检测脚本 ping_test.sh,内容如下:
#/bin/bash
lv_ip_local=`ifconfig eth1 | head -2 | tail -1 | awk -F' ' '{print $2}' | awk -F':' '{print $2}'`
lv_ip_dest=""
lv_ip_ping="127.0.0.1"
echo "param1=" $1
if [[ "$1" = "" ]] ; then
lv_ip_ping="127.0.0.1"
lv_ip_dest="$lv_ip_local"
else
lv_ip_dest="$1"
if [[ "$1" = "$lv_ip_local" ]] ;then
lv_ip_ping="127.0.0.1"
else
lv_ip_ping="$1"
fi
fi
echo $lv_ip_local
echo $lv_ip_dest
echo $lv_ip_ping
while true
do
lv_date=`date +"%F"`
echo `date +"%F %T" ; ping -c 1 ${lv_ip_ping}|grep -i from 2>&1` >> ${lv_date}_${lv_ip_local}_${lv_ip_dest}.txt
sleep 1
done
4.加入到后台进程进行执行,避免关闭窗口脚本停止运行
nohup ./ping_test.sh "127.0.0.1" &>/dev/null &
5.脚本进程及关闭方法
ps -ef | grep ping_test.sh
kill -9 进程PID