当我的主机关闭时发出警报

alarm when my host is down

我有这个 bash 脚本,它可以帮助我知道我的某些服务器何时停机。

 #!/bin/bash
HOSTS="192.168.11.154 192.168.11.155"
COUNT=5 

pingtest(){
  for myHost in "$@"
  do
    ping -c "$COUNT" "$myHost" && return 1
  done
  return 0
}

if pingtest $HOSTS
then
  # 100% failed
zenity --warning --text="<span size=\"xx-large\">WARNING</span>\n\n\n<b>*IP*</b> is down" --title="SERVER DOWN" --ok-label="OKAY"
fi

所以,我怎么知道具体的服务器宕机了? 我想交换 IP 到 192.168.11.154 或 192.168.11.155(取决于什么不可用)

最好将 ips 存储在两个单独的变量中并调用函数两次:-

HOSTS1="192.168.11.154"
HOSTS2="192.168.11.155"

if pingtest $HOSTS1
 //

if pingtest $HOSTS2
#!/bin/bash

HOSTS="192.168.11.154 192.168.11.155"
COUNT=5

pingtest () {
  ping -c "$COUNT" "" || return 1
}

for H in $HOSTS; do
  if ! pingtest $H; then
    zenity --warning --text="<span size=\"xx-large\">WARNING</span>\n\n\n<b>${H}</b> is down" --title="SERVER DOWN" --ok-label="OKAY"
  fi
done