sh 一个简单的bash脚本,用于在Unix / Linux上使用ping观察数据包丢失

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 一个简单的bash脚本,用于在Unix / Linux上使用ping观察数据包丢失相关的知识,希望对你有一定的参考价值。

#!/bin/bash
# Packets Loss Watch
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
#
# Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
# Copyleft 2013 Stephen Larroque
# This script is licensed under GNU GPL version 2.0 or above
#
# This script was inspired by a nixCraft script http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
#
# For more complex needs, take a look at:
# - SmokePing: http://oss.oetiker.ch/smokeping/
# - DropWatch: http://humblec.com/dropwatch-to-see-where-the-packets-are-dropped-in-kernel-stack/
# - sjitter: http://www.nicolargo.com/dev/sjitter/
# - iperf: http://iperf.fr/
# -------------------------------------------------------------------------

#=== PARAMETERS change them here
# add ip / hostname separated by while space
HOSTS="www.google.com"
# no ping request
COUNT=20
# email report when
#SUBJECT="Ping failed"
#EMAILID="me@mydomain.com"

#=== Local vars (do not change them)
# Cron-friendly: Automaticaly change directory to the current one
cd $(dirname "$0")

# Current script filename
SCRIPTNAME=$(basename "$0")

# Current date and time
today=$(date '+%Y-%m-%d')
currtime=$(date '+%H:%M:%S')

#=== Help message
if [[ "$@" =~ "--help" ]]; then
  echo "Usage: bash $SCRIPTNAME
Check the rate of packets loss and output the result in a file named plwatch.txt in the same directory as this script.
Note: this script is cron-friendly, so you can add it to a cron job to regularly check your packets loss.
"
	exit
fi

#=== Main script
for myHost in $HOSTS
do
  msg=$(ping -c $COUNT $myHost | grep 'loss')
  echo "[$today $currtime] ($myHost $COUNT) $msg" >> plwatch.txt
  #count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  #if [ $count -eq 0 ]; then
    # 100% failed
  #  echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  #fi
done

以上是关于sh 一个简单的bash脚本,用于在Unix / Linux上使用ping观察数据包丢失的主要内容,如果未能解决你的问题,请参考以下文章

在bash中管理csv文件中读取动态字段的Unix权限

sh 用于检查MySQL是否正在运行的简单bash脚本。

sh Shell:用于UNIX系统的Bash CheatSheet

Linux/Unix Bash 脚本如何知道自己的 PID?

unix下source的使用

sh 简单的bash脚本,用于获取并将执行文件夹中的所有repos拉到它们所在的最新分支