提示:
1. 你可以使用ping命令 ping -c10 www.baidu.com
2. 发邮件的命令是 echo "邮件内容" |mail -s "主题" abc@139.com
3. 脚本可以搞成死循环,每隔30s检测一次
- #!/bin/bash
- ##检测主机存活状况
- while :;
- do
- ping -c10 123.23.11.21
- if [ $? -ne 0 ]
- then echo "这台主机有问题,请及时处理"|mail -s "11.21主机无法ping通" abc@139.com
ping -c10 123.23.11.21 >1.txt
while grep -q '100% packet loss' 1.txt
do
echo "主机疑似宕机"|mail -s "Warning" abc@139.com
done
sleep 30
stat=`ping -c5 123.23.11.21 |tail -2|head -1|awk -F ',' '{print $3}' |awk '{print $1}'|sed 's/%//'`
while [ $stat -eq 100 ]
do
echo "The ip of 123.23.11.21 is down"|mail -s "Warning" abc@139.com
sleep 30
done
- #!/bin/bash
- $ip=123.23.11.21
- while : ;
- do
- ping -c10 www.baidu.com
- if [ $? -ne 0 ]
- then echo "$ip is error"|mail -s "$ip is error" abc@139.com
- fi
- sleep 30
- done
- #!/bin/bash
- while :;do
- ping=`ping -c 5 www.qq.com`
- loss=`echo $ping|grep '100% packet loss' |awk -F ',' '{print $3}'|awk '{print $1}'`
- if [ ! -z $loss ];then
- echo $loss
- echo "down " | mail -s abc@139.com
- fi
- sleep 30
- done
while :
do
ping -c10 www.baidu.com
sleep 30
if [ -n `ping -c10 www.baidu.com |grep 'timeout'` ]
then
echo 'baidu is dead' |mail -s 'down' abc@139.com
fi
done
while :; do
g=`ping -c10 192.168.0.201 |awk 'NR==14'|awk '{print $4}'`
if [[ $g -eq 10 ]]; then
echo "The Hosts is OK!"
else
echo "The Hosts is down!" |mail -s "主题" abc@139.com
fi
sleep 30
done
ping -c10 123.23.11.21>1.txt
n=`wc -l 1.txt`
while :;do
if [ $n -ne 10 ]
then
echo "宕机”|mail -s "11.21" abc@139.com
fi
sleep 30
done
while :;do
a=`ping -c3 www.baidu.com| wc -l`
if ((a==8));
then
echo "system load is high"
fi
sleep 1
done
a = ` ping www.baidu.com | awk -F ' ' '/%/ {print $(NF-4)}'|cut -c 1 `
if ((a > 0));then
echo "The Host has shutdown" |mail -s "system load" aming@139.com
fi
done
需要打印倒数第五段,这样更加精确
- #! /bin/bash
- while true;do
- ping -c 10 123.23.11.21
- if [ $? -ne 0 ];then
- echo "123.23.11.21 down"| /bin/mailx -s "123.23.11.21 is down" 775356210@qq.com;
- break
- else sleep 30
- fi
- done
while :;
do n=`ping -c10 10.10.13.247|grep -c time=`
if [ $n -eq 0 ]
then echo "the machine is down"|mail -s "warn" chenconghe@benco.com.cn
else exit
fi
sleep 30
done
#设计一个脚本,监控远程的一台机器(假设ip为192.168.100.2)的存活状态,当发现宕机时发一>
封邮件给你自己。#为了方便测试加两条输出语句,以示有反应。
while :;do
ping -c 3 192.168.100.2 > /dev/null
i=`echo $?`
if [ $i -ne 0 ];then
echo "danger" |mail -s "192.168.100.2 have died"
echo "192.168.100.2 have died"
else
echo "this is keepalive"
fi
sleep 30
done
#!/bin/bash
while :
do
n=`ping 123.23.11.21 -c5 |grep 'packet loss'|awk -F ',' '{print $4}'|awk '{print $1}'`
if [ $n == "100%" ]
then
echo "service 123.23.11.21 is down" |mail -s "server is down" abc@139.com
sleep 30
fi
done
#!/bin/bash
#Description: This script is to monitor remote host and decide if it's alive or not.
#Author: Jiazhi Yang
#Date: 10/11/2016
#Script Name: monitorremotehost_20161110.sh
#定义所指定一个确定的IP地址,如Google的DNS
ip='8.8.8.8'
#让远程主机去ping Google的DNS,倘若返回0则表示云主机时存活状态,否则就发邮件给指定的邮箱地址
while :;do
ping -c 10 $ip >/dev/null
if [[ $? == 0 ]];then
echo "System is alive!" >/dev/null
else
echo "System is dead!!" |mail -s "System status" xxxx@139.com
fi
sleep 30
done
#!/bin/bash
ip=123.23.11.21
baidu=www.baidu.com
mail=abc@139.com
while:;do
ping -c $baidu
if(($?==0));then
echo "$ip is downtime"|mail -s "快去" $mail
fi
sleep 30
done
#Author: Jiazhi Yang
#Date: 10/11/2016
#Script Name: monitorremotehost_20161110.sh
#定义主机的IP地址
host='123.23.11.21'
#获取ping 主机的返回包
while :
do
pingcounts=`ping -c 3 $host |grep 'receive' |awk -F',' '{print $2}' |awk '{print $1}'`
if [ $pingcounts == '0' ];then
echo "System $host is dead!!" |mail -s "System status" xxxx@139.com
else
echo "System $host is alive!!"
fi
sleep 30
done
#!/bin/bash
ip=123.23.11.21
email=abc@139.com
cheak_ok() {
ping -c 10 ${ip}
if [ $? != 0 ]
then
echo "$ip宕机" |mail -s "$ip宕机" $email
sleep 30
cheak_ok
else
sleep 30
cheak_ok
fi
}
cheak_ok
[root@AAA ~]# vi 3.sh
#!/bin/bash
while :
do
ping -c5 192.168.0.109>/dev/dull
if [ $? -ne 0 ]
then
echo "ip is bad"|mail -s "192.168.0.103 is bad" jishan1984@163.com
fi
sleep 30
done
[root@AAA ~]# sh 3.sh &
[2] 2278
[root@AAA ~]# vi 3.sh
#!/bin/bash
while :
do
ping -c5 192.168.0.109>/dev/dull
if [ $? -ne 0 ]
then
echo "ip is bad"|mail -s "192.168.0.103 is bad" jishan1984@163.com
fi
sleep 30
done
[root@AAA ~]# sh 3.sh &
[2] 2278
#!/bin/bash
ip='192.168.0.19'
mail=aiker@gzedu.ml
while true
do
ping -c4 $ip > /dev/null
if [ $? -ne 0 ]
then
echo "`date +%F-%T` $ip is down" | mail -s " $ip is down" $mail
fi
sleep 30
done
#!/bin/bash#检查宕机#20130927while :;doping -c10 123.23.11.21>/dev/dullif($?!=0)then echo "邮件内容" |mail -s "主题" abc@139.comfisleep 30done
编辑回复