好的,所以我需要不断地监视多个路由器和计算机,以确保它们保持在线。我已经找到了一个很棒的脚本here,如果一个ip地址不能被pinged,它会通过咆哮通知我(这样我就可以在我的手机上得到即时通知)。我一直在尝试修改脚本,以ping多个地址,但运气不佳。当脚本一直在监视在线服务器时,我很难弄清楚如何ping掉服务器。任何帮助都将不胜感激。我没有做过太多的shell脚本,所以这对我来说是很新的。
谢谢
#!/bin/sh
#Growl my Router alive!
#2010 by zionthelion73 [at] gmail . com
#use it for free
#redistribute or modify but keep these comments
#not for commercial purposes
iconpath="/path/to/router/icon/file/internet.png"
# path must be absolute or in "./path" form but relative to growlnotify position
# document icon is used, not document content
# Put the IP address of your router here
localip=192.168.1.1
clear
echo 'Router avaiability notification with Growl'
#variable
avaiable=false
com="################"
#comment prefix for logging porpouse
while true;
do
if $avaiable
then
echo "$com 1) $localip avaiable $com"
echo "1"
while ping -c 1 -t 2 $localip
do
sleep 5
done
growlnotify -s -I $iconpath -m "$localip is offline"
avaiable=false
else
echo "$com 2) $localip not avaiable $com"
#try to ping the router untill it come back and notify it
while !(ping -c 1 -t 2 $localip)
do
echo "$com trying.... $com"
sleep 5
done
echo "$com found $localip $com"
growlnotify -s -I $iconpath -m "$localip is online"
avaiable=true
fi
sleep 5
done发布于 2011-01-17 06:53:10
最简单的方法是用另一个创建N个进程的脚本包装这个脚本。假设您的脚本名为"watchip",然后将文本放入另一个脚本中
watchip 10.0.1.1 &
watchip 10.0.1.2 &
watchip 10.0.1.3 &
etc并在watchip中将localip设置为$1。
发布于 2011-01-17 07:28:07
我认为没有必要运行多个脚本。这是一个通用脚本,用于监视IP地址列表并记录ping成功的变化...
#!/bin/bash
set 10.0.0.1 10.0.0.2 # etc
trap exit 2
while true; do
i=1
for ipnumber in "$@"; do
statusname=up$i
laststatus=${!statusname:-0}
ping -c 1 -t 2 $ipnumber > /dev/null
ok=$?
eval $statusname=$ok
if [ ${!statusname} -ne $laststatus ]; then
echo status changed for $ipnumber
if [ $ok -eq 0 ]; then
echo now it is up
else
echo now it is down
fi
fi
i=$(($i + 1))
done
sleep 5
done发布于 2011-01-17 06:54:47
将localip=192.168.1.1更改为:
localip=$1这允许将IP地址作为命令行参数传入。然后,您可以在不同的IP地址中运行脚本的多个副本。然后,您可以创建一个主脚本来运行监控脚本的多个副本。假设您发布的脚本是monitor.sh
#!/bin/sh
monitor.sh 192.168.1.1 &
monitor.sh 192.168.2.2 &
monitor.sh 192.168.3.3 &
waithttps://stackoverflow.com/questions/4708631
复制相似问题