博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个刚刚编写的监控主机是否可以ping通的脚(转)
阅读量:2450 次
发布时间:2019-05-10

本文共 2844 字,大约阅读时间需要 9 分钟。

一个刚刚编写的监控主机是否可以ping通的脚(转)[@more@]

初学shell,一个刚刚编写的监控主机是否可以ping通的脚本,如果某个主机ping不通,发出邮件,并记录,如果再次能ping通,再次发出邮件,通知,并可设置声音报警。

代码:

#!/bin/sh

# set -x

#######################################################

PingConf='/opt/monitor/Iptest'

PingCantrech='/opt/monitor/IpcannotReach'

#File where the log will be stored

LogDir='/opt/monitor/Log'

#Define the interval time

INTERVAL='10'

#Define the alarm process

User1='xiaoli'

User2='xiaozhang'

YouNeed='/opt/monitor/YouNeed'

########################################################

proc_alarm()

{

while read IpAddress

do

#ingore any hash signs

case $IpAddress in

#*)

;;

*)

ping $IpAddress -c 5 >errtmp

if [ $? = 0 ] ; then

rm errtmp

else

rm errtmp

ErrTime=`date +%Y/%m/%d/%H:%M:%S`

echo "$ErrTime"": ""$IpAddress"" cann't be connected">>$LogDir

echo "$IpAddress">>tmp11

sleep 1

mail -s "$IpAddress"_Can_not_reach xiaoli@abced.com echo "from $User1"": ""from $User2" >$YouNeed

echo "$ErrTime"": ""$IpAddress"" off " >>$YouNeed

audioplay -v 50 -p speaker /opt/monitor/space.au

fi

;;

esac

done < $PingConf

}

##########################################################

proc_unalarm()

{

while read IpAddress

do

#ingore any hash signs

case $IpAddress in

#*)

;;

*)

ping $IpAddress -c 5 >errtmp

if [ $? = 0 ] ; then

rm errtmp

ErrTime=`date +%Y/%m/%d/%H:%M:%S`

echo "$ErrTime"": ""$IpAddress"" can be connected">>$LogDir

sleep 1

mail -s "$IpAddress"_OK_le xiaoli@abced.com echo "from $User1"": ""from $User2" >$YouNeed

echo "$ErrTime"": ""$IpAddress"" on " >>$YouNeed

echo $IpAddress >>tmp12

# audioplay -v 50 -p speaker /opt/monitor/space1.au

else

rm errtmp

fi

;;

esac

done < $PingCantrech

}

##########################################################

# execute the proc_main function every the specified time INTERVAL

uniq $PingCantrech > tmp17

cat tmp17 >$PingCantrech

rm tmp17

if [ -e $PingCantrech ]

then

proc_unalarm

else

echo "$PingCantrech" is null

fi

while [ "1" -eq "1" ]

do

# execute the proc_main function

##########################

proc_alarm

##########################

if [ -e tmp11 ]

then

echo "$MYFILE" is nomal

if [ -e "$PingCantrech" ]

then

echo "$PingCantrech" is nomal

cat tmp11 >>$PingCantrech

uniq $PingCantrech > tmp18

cat tmp18 >$PingCantrech

rm tmp18

proc_unalarm

cat tmp11 >$PingCantrech

else

cat tmp11 >$PingCantrech

proc_unalarm

fi

rm tmp11

MYFILE='tmp12'

if [ -e "$MYFILE" ]

then

comm -23 $PingCantrech tmp12 >tmp13

rm tmp12

cat tmp13 >$PingCantrech

rm tmp13

else

echo "$MYFILE" is null

cat tmp11 >$PingCantrech

fi

elif

echo "$MYFILE" is null

then

echo "$MYFILE" is null

if [ -e "$PingCantrech" ]

then

proc_unalarm

else

echo "$MYFILE" is null

fi

fi

######################################

# suspend execution for INTERVAL seconds

sleep $INTERVAL

don

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8225414/viewspace-944885/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8225414/viewspace-944885/

你可能感兴趣的文章
n字节对齐 代码_大但正确对齐和优化的代码比每指令/操作码打包的字节少的代码快...
查看>>
火狐 旧版sync同步服务_如何设置Firefox Sync?
查看>>
python的smiley_SmIley面对iPhone
查看>>
电脑备份iphone_如何在Linux上备份iPhone?
查看>>
latex在编译公式_如何在Linux上编译Latex
查看>>
同步主目录
查看>>
hadoop单机映射_如何在命令行中设置Hadoop的映射器和缩减器的数量?
查看>>
安装fedora在u盘_如何在Fedora Linux上安装Scala
查看>>
如何使用ffmpeg将flv转换为mp4?
查看>>
ocaml_如何使用OCaml作为脚本语言?
查看>>
fedora下使用deb_如何在Fedora上安装.deb软件包?
查看>>
刘昕 hkust_在Linux上配置HKUST的sMobileNet
查看>>
win api发送键盘消息_使用Win32 API将消息发送到其他Windows
查看>>
Python“ for”循环(定迭代)
查看>>
软件测试应届生没有项目经历_为什么有些软件项目网站糟透了,而另一些却没有
查看>>
python入门测试教程_Python测试入门
查看>>
python概率编程_Python中的概率编程
查看>>
Python中的运算符和表达式
查看>>
读写csv文件python_用Python读写CSV文件
查看>>
python super_使用Python super()增强您的课程
查看>>