2

好的,所以我需要不断监控多个路由器和计算机,以确保它们保持在线。我在这里找到了一个很棒的脚本,如果无法ping通单个IP,它将通过咆哮通知我(这样我就可以在手机上获得即时通知)。我一直在尝试修改脚本以 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
4

4 回答 4

1

最简单的方法是将此脚本与另一个创建 N 个进程的脚本包装在一起。假设您的脚本称为“watchip”,然后将文本放入另一个脚本

watchip 10.0.1.1 &
watchip 10.0.1.2 &
watchip 10.0.1.3 &
etc

并在 watchip 中将 localip 设置为 $1。

于 2011-01-16T22:53:09.877 回答
1

我认为没有必要运行多个脚本。这是一个通用脚本,用于监视 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-16T23:28:06.713 回答
0

保留两个数组。一个有可用IP;另一个不可用的。当它们的状态发生变化时,将它们移动到另一个阵列。无需多个后台进程。

我省略了日志记录的东西。您可以重新添加它。这是未经测试的代码。

available=(192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4)
unavailable=()

while true
do
    for index in ${!available[@]}
    do
        if ! ping -c 1 -t 2 ${available[index]}
        then
            growlnotify  -s -I $iconpath -m "${available[index]} is offline"
            unavailable+=(${available[index]})
            unset "available[index]"
        fi
    done

    for index in ${!unavailable[@]}
    do
        if ping -c 1 -t 2 ${unavailable[index]}
        then
            growlnotify  -s -I $iconpath -m "${unavailable[index]} is back online"
            available+=(${unavailable[index]})
            unset "unavailable[index]"
        fi
    done
done
于 2011-01-16T23:41:02.830 回答
0

更改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 &
wait
于 2011-01-16T22:54:46.600 回答