Try to write a ping confirmation script appropriately

Try to write a ping confirmation script appropriately

Requirements

--The target IP is described in ʻip_node.list. --If there is any packet loss`, you will be notified by email. --The network settings and mail settings have been completed.

Target IP list

I will describe it as follows.

192.168.11.21
192.168.11.29
192.168.11.69
192.168.11.88
192.168.11.99

Script body

Like this.

bash


#!/bin/bash
LANG=C;
export LANG;
:> ~/mail.dat;
while read line;do
  _RET_=`ping -c 5 -i 1 -w 10 -s 32 ${line} | grep 'loss' | grep -oP '[0-9]+%'`;
  echo "${line} ${_RET_} loss";
  [ "0%" == "${_RET_}" ] && continue;
  echo "${line} ${_RET_} loss" >> ~/mail.dat;
done <~/ip_node.list;
_RET_=`du ~/mail.dat | awk '{print $1}' | xargs expr`;
[ ! 0 -eq ${_RET_} ] && mail -s "IP packet loss alert." -r [from addr] [to addr] < ~/mail.dat;
exit 0;

Script body (with comments)

bash


#!/bin/bash
LANG=C;
export LANG;   #When the Ping result is displayed in Japanese, the regular expression of grep is troublesome, so LANG=Fixed to C

:> ~/mail.dat;   #Set the body for email notification to 0 bytes

while read line;do   #Enter the file in the done part below$Variables for each line in line

  _RET_=`ping -c 5 -i 1 -w 10 -s 32 ${line} | grep 'loss' | grep -oP '[0-9]+%'`;   #Perform Ping, adjust options etc. to your liking, and get the matched part with gpre

  echo "${line} ${_RET_} loss";

  [ "0%" == "${_RET_}" ] && continue;   # 0%Other than the notification, this is fine

  echo "${line} ${_RET_} loss" >> ~/mail.dat;   #Write for notification

done <~/ip_node.list;

_RET_=`du ~/mail.dat | awk '{print $1}' | xargs expr`;   #Get the size of the file for notification

[ ! 0 -eq ${_RET_} ] && mail -s "IP packet loss alert." -r [from addr] [to addr] < ~/mail.dat;   #If non-zero, notify

exit 0;

Recommended Posts

Try to write a ping confirmation script appropriately
Creating a shell script to write a diary
Write a script to convert a MySQL dump to TSV
Try to select a language
I'll never forget how to write a shell script, don't forget! !!
How to write a Python class
Try to draw a Bezier curve
Write standard output to a file
Write a batch script with Python3.5 ~
Try to write a program that abuses the program and sends 100 emails
Write a script to calculate the distance with Elasticsearch 5 system painless
Try to make a kernel of Jupyter
[Ubuntu] How to execute a shell script
A simple IDAPython script to name a function
I made a script to display emoji
Script to create a Mac dictionary file
How to run a Maya Python script
Try to draw a life curve with python
Why does Python have to write a colon?
How to write a ShellScript Bash for statement
Try to create a new command on linux
I wrote a script to upload a WordPress plugin
How to write a named tuple document in 2020
[Go] How to write or call a function
I want to write to a file with Python
How to write a ShellScript bash case statement
Try to make a Python module in C language
Try to make a command standby tool with python
Try to dynamically create a Checkbutton with Python's Tkinter
(Python) Try to develop a web application using Django
I want to write in Python! (2) Let's write a test
I made a script to put a snippet in README.md
How to write a list / dictionary type of Python3
[GCP] Try a sample to authenticate users with Firebase
[Python] How to write a docstring that conforms to PEP8
A sample to try Factorization Machines quickly with fastFM
[Python] A memo to write CSV vertically with Pandas
Machine learning beginners try to make a decision tree
How to create a simple TCP server / client script
Write code to Unit Test a Python web app
Generate a bash script to add Datadog monitor settings