【问题标题】:Modify shell script to monitor/ping multiple ip addresses修改 shell 脚本以监控/ping 多个 IP 地址
【发布时间】:2011-01-16 22:48:51
【问题描述】:

好的,所以我需要不断监控多个路由器和计算机,以确保它们保持在线状态。我找到了一个很棒的脚本here,如果无法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

【问题讨论】:

    标签: macos shell ping growl


    【解决方案1】:

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

    watchip 10.0.1.1 &
    watchip 10.0.1.2 &
    watchip 10.0.1.3 &
    etc
    

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

    【讨论】:

      【解决方案2】:

      我认为没有必要运行多个脚本。这是一个通用脚本,用于监控 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
      

      【讨论】:

      • 谢谢,但我需要知道它是在线还是离线,而不是从一种状态变为另一种状态。多个脚本会导致资源使用量显着增加吗?
      • 查看我的回答,了解使用数组的类似技术。
      • @Alex:好的,好的,我已经修改了脚本,所以你可以看到如何向上或向下检查。但在某些时候你只需要阅读“man bash”。在后台运行多个脚本的问题不是资源消耗,而是复杂性。这很丑,检查和杀死它们会很烦人,你不能轻易记录它们的输出,很难调试等等。
      • 哇,太好了,非常感谢。虽然我不知道如何在启动时验证在线 ip 和离线 ip。它只显示那些离线的。在它首先离线之前,它不会显示它在线。
      • 好吧,这可能要求很多,但我也想知道 ip 名称(即路由器地下室、pc 1 等)。我想如果很难的话,我可能会坚持使用多个脚本。再次感谢
      【解决方案3】:

      将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
      

      【讨论】:

        【解决方案4】:

        保留两个数组。一个有可用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
        

        【讨论】:

        • 我不确定发生了什么,没有通知它只是停止,在第二次 ping 之后输出:“输入通知描述,后跟换行符,然后是 Ctrl-D(结束文件)。要取消,请按 Ctrl-C。"
        • @Alex:这可能是因为我没有包含您原来的一些代码。除了日志相关的代码,我没有设置变量iconpath,这可能会导致错误。此外,您可能希望重定向ping 的输出:ping ... > /dev/null。
        猜你喜欢
        • 2014-07-27
        • 2011-11-26
        • 2013-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        相关资源
        最近更新 更多