Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple bash script to watch packets loss using ping on Unix/Linux
# Packets Loss Watch
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
# Copyright (c) 2006 nixCraft project <>
# Copyleft 2013 Stephen Larroque
# This script is licensed under GNU GPL version 2.0 or above
# This script was inspired by a nixCraft script
# For more complex needs, take a look at:
# - SmokePing:
# - DropWatch:
# - sjitter:
# - iperf:
# -------------------------------------------------------------------------
#=== PARAMETERS change them here
# add ip / hostname separated by while space
# no ping request
# email report when
#SUBJECT="Ping failed"
#=== 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.
#=== Main script
for myHost in $HOSTS
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

This comment has been minimized.

Copy link

Muminur commented Jun 4, 2018

A cleaner version would be like below

packet=$(ping -c 3 | grep "packet loss" | awk -F ',' '{print $3}' | awk '{print $1}')
echo "$packet" >> watch.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.