Skip to content

Instantly share code, notes, and snippets.

@Marnek
Created June 14, 2019 16:19
Show Gist options
  • Save Marnek/6b3346cc0ad2c6eae6d6e36a9ce0591c to your computer and use it in GitHub Desktop.
Save Marnek/6b3346cc0ad2c6eae6d6e36a9ce0591c to your computer and use it in GitHub Desktop.
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.
#!/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