Created
June 14, 2019 16:19
A simple bash script that pingfloods an array of IPs/hostnames and outputs a JSON of the status of each item in the array, as well as the last date and time of the execution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SERVERS=( 192.168.12.5 192.168.12.10 192.168.12.15 192.168.19.1 192.168.20.1 ) | |
STATUS=() | |
ArrayID=0 | |
Entries=${#SERVERS[@]} | |
Counter=0 | |
LastDate=`date +"%Y-%m-%d"` | |
LastTime=`date +"%H:%M"` | |
for i in "${SERVERS[@]}"; do | |
STATUS[$ArrayID]="OFFLINE" | |
((ArrayID++)) | |
done | |
OUTPUT=$(for i in "${SERVERS[@]}"; do | |
# Non-root users can't pingflood. Adapt the interval / -f option | |
timeout 0.2 ping -c 2 -f -i 0.1 $i > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
STATUS[$Counter]="ONLINE" | |
fi | |
echo "\"$i\":\"${STATUS[$Counter]}\"" | |
((Counter++)) | |
done) | |
TIMESTAMP=$( | |
echo "\"date\":\"$LastDate\"" | |
echo "\"time\":\"$LastTime\"" | |
) | |
echo -e "{$OUTPUT, $TIMESTAMP}" | sed ':a;N;$!ba;s/\n/, /g' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment