好吧,所以我需要不断监控多个路由器和计算机,以确保它们保持在线。我找到了一个很棒的脚本here如果无法 ping 通单个 IP,它将通过 growl 通知我(这样我就可以在手机上收到即时通知)。我一直在尝试修改脚本以 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
最佳答案
我认为没有必要运行多个脚本。这是一个通用脚本,用于监视 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
关于macos - 修改shell脚本来监控/ping多个ip地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4708631/