【问题标题】:Bash script to ping a IP and if the ms is over 100 print a echo msg用于 ping IP 的 Bash 脚本,如果 ms 超过 100,则打印 echo msg
【发布时间】:2015-01-24 21:15:09
【问题描述】:

我是 bash 脚本的新手。

我需要一个脚本来获取对 IP 的 ping 毫秒数,如果时间超过 100,它将打印一条回显消息。

例如,让我们使用 google ip 8.8.8.8 来完成

你能帮帮我吗?

编辑:

好的,怎么弄成这样:

#!/bin/sh

echo '>> Start ping test 2.0'

/bin/ping 8.8.8.8 | awk -F' |=' '$10=="time"'

if [$11>100]
    then
        echo "Slow response"
    else
        echo "Fast response"
fi

【问题讨论】:

  • 您在尝试自己编写此脚本时遇到了哪些问题?
  • 我抓不到女士。

标签: bash shell debian sh bin


【解决方案1】:

好的...首先,您不是在编写 bash 脚本,您的脚本是使用 #!/bin/sh 调用的,因此即使您的系统使用 bash 作为其系统 shell,它也以 sh 兼容模式运行。所以你不能使用bashisms。改为如下所示编写您的脚本。

所以...在我看来,如果您希望您的 ping 具有由您的脚本处理的输出,那么 ping 需要实际退出。你的if 永远不会被处理,因为ping 永远不会停止运行。此外,awk 脚本中的$11 与shell 脚本中的$11 不同。所以这样的事情可能会起作用:

#!/bin/bash

while sleep 5; do
  t="$(ping -c 1 8.8.8.8 | sed -ne '/.*time=/{;s///;s/\..*//;p;}')"
  if [ "$t" -gt 100 ]; then
    # do something
  else
    # do something else
  fi
done

在 shell(或 bash)中,这个 while 循环将每五秒运行一次 ping,只发送一个数据包(-c 1),并使用sed 解析其输出。 sed 脚本的工作方式如下:

  • /.*time=/{...} - 查找包含时间的行并在该行的花括号中运行内容...
  • s/// - 将之前找到的表达式(时间)替换为空(从行中删除)
  • s/\..*// - 替换从第一个句点到行尾的所有内容(因为 shell 数学只处理整数)
  • p - 并打印该行的剩余数据。

另一种处理方法是将ping 的输出解析为,而不是为每个测试生成一个新的ping 进程。例如:

#!/bin/bash

ping -i 60 8.8.8.8 | while read line; do
  case "$line" in
  *time=*ms)
    t=${line#.*=}   # strip off everything up to the last equals
    t=${t% *}       # strip off everything from the last space to the end
    if [[ (($t > 100)) ]]; then
      # do something
    else
      # do something else
    fi
    ;;
done

这些解决方案有点问题,因为它们无法在连接完全消失时报告。但也许你也可以调整它们来处理这种情况。

请注意,这些可能不是您的最佳解决方案。如果你真的想要一个监控系统,像NagiosIcingaMunin 等更大规模的东西是一个不错的选择。

对于像这样的小规模 ping 监控,你可能还想看看fping

【讨论】:

    【解决方案2】:

    您需要对 ping 输出进行一些转换才能获得毫秒的实际数字。

    首先,为了简单起见,使用ping-c 1 标志只发送一个数据包。

    ping 的输出将如下所示:

    PING 8.8.8.8 (8.8.8.8): 56 data bytes
    64 bytes from 8.8.8.8: icmp_seq=0 ttl=59 time=41.101 ms
    
    --- 8.8.8.8 ping statistics ---
    1 packets transmitted, 1 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 41.101/41.101/41.101/0.000 mss
    

    由于您想要 '41.101' 片段,您需要解析出 第二行倒数第二个元素

    要提取第二行,您可以使用awk 中的FNR 变量,要获取倒数第二列,您可以使用NF(字段数)变量。

    ping -c 1 8.8.8.8 |  awk 'FNR == 2 { print $(NF-1) }'
    

    这将为您提供time=41.101,若要仅获取数字,请使用cut 提取等号后的字段

    ping -c 1 8.8.8.8 |  awk 'FNR == 2 { print $(NF-1) }' | cut -d'=' -f2
    

    【讨论】:

    • 为什么要运行额外的管道? split()sub() 应该足以处理您对 cut 所做的事情,不是吗?
    【解决方案3】:

    这就是我所做的,以跟踪缓慢的 ping 时间,并且如果您想要的话,还会向我或任何人发送邮件。

    #!/bin/bash
    
    if [ "$#" -ne 1 ]; then
        echo "You must enter 1 command line arguments - The address which you want to ping against"
    exit
    fi
    
    hostname=$(hostname)
    
    while true; do
    
    RESULT=$(ping -c 1 $1 | awk -v time="$(date +"%Y-%m-%d %H:%M:%S")" -Ftime= 'NF>1{if ($2+0 > 1) print $1 $2 $4 $3 $5 " "time }')
    
    if [ "$RESULT" != "" ]; then
    echo $RESULT >> pingscript.log
    echo $RESULT | mail -s "pingAlert between $hostname -  $1" foo@bar.com
    fi
    
    sleep 2
    done
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2022-01-04
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 2020-04-11
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多