修改Shell脚本以监视/ping多个IP地址 [英] Modify shell script to monitor/ping multiple ip addresses

查看:670
本文介绍了修改Shell脚本以监视/ping多个IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我需要不断监视多台路由器和计算机,以确保它们保持在线状态.我在此处找到了一个很棒的脚本,该脚本会通过咆哮声通知我(所以我可以如果无法ping通单个IP,则会在我的手机上获得即时通知).我一直在尝试修改脚本以ping通多个地址,但运气不佳.我在尝试弄清如何对关闭的服务器执行ping操作时遇到了麻烦,而脚本一直在监视在线服务器.任何帮助将不胜感激.我没有做太多的Shell脚本编写工作,所以这对我来说是很新的.

Alright so I need to constantly monitor multiple routers and computers, to make sure they remain online. I have found a great script here that will notify me via growl(so i can get instant notifications on my phone) if a single ip cannot be pinged. I have been attempting to modify the script to ping multiple addresses, with little luck. I'm having trouble trying to figure out how to ping a down server while the script keeps watching the online servers. any help would be greatly appreciated. I haven't done much shell scripting so this is quite new to me.

谢谢

#!/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

推荐答案

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

The simplest approach is to wrap this script with another one that creates N processes. Assume your script is called "watchip", then put into another script the text

watchip 10.0.1.1 &
watchip 10.0.1.2 &
watchip 10.0.1.3 &
etc

,然后在watchip中将localip设置为$ 1.

and set localip to $1 inside watchip.

这篇关于修改Shell脚本以监视/ping多个IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆