Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  22] [ 0]  / answers: 1 / hits: 151852  / 2 Years ago, mon, june 6, 2022, 1:53:22

I have 10 IP numbers which I have to ping daily for checking , How I can do that by using BASH script. So that I can automate that task by using cron. I want BASH script only.



Thank you.


More From » bash

 Answers
4

As your ip range has no symmetry and there are only 10 nodes, I would suggest to list them in a text file. I am considering the file containing the list is list.txt which contains list of ip one at each line as shown below,



10.12.13.14
172.15.48.3
192.168.45.54
...
48.114.78.227


You can use this script,



#!/bin/bash
# Program name: pingall.sh
date
cat /path/to/list.txt | while read output
do
ping -c 1 "$output" > /dev/null
if [ $? -eq 0 ]; then
echo "node $output is up"
else
echo "node $output is down"
fi
done


To update the running status of your nodes at an interval of 30 mins use at crontab,



*/30 * * * * /path/to/pingall.sh > /path/to/log.txt


Output of log.txt



$ cat /path/to/log.txt
Fri Jan 31 15:06:01 IST 2014
node 10.12.13.14 is up
node 172.15.48.3 is up
node 192.168.45.54 is up
...
node 48.114.78.227 is down

[#27202] Tuesday, June 7, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
otatorm

Total Points: 218
Total Questions: 113
Total Answers: 124

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;