Skip to content

Instantly share code, notes, and snippets.

@dimitriacosta
Created April 8, 2020 15:50
Show Gist options
  • Save dimitriacosta/4eb2b7721cd4586cc3dfff2040766b29 to your computer and use it in GitHub Desktop.
Save dimitriacosta/4eb2b7721cd4586cc3dfff2040766b29 to your computer and use it in GitHub Desktop.
Check multiple hosts to see if they are reachable
#!/usr/bin/env bash
# Verifies that a hosts.txt file exists
# This file should contain a list of IP to pinged
if [[ ! -e ./hosts.txt ]]; then
echo "Error: The file hosts.txt doesn't exists in this directory." >&2
exit 1
fi
TIMESTAMP=$(date -u +%FT%T)
EXIT_STATUS=0
# Print header
echo -e "Date\t\t\tHost\t\tStatus"
for HOST in $(cat ./hosts.txt)
do
# Ping each ip in the hosts.txt file
ping -c 1 ${HOST} -t 3 &> /dev/null
if [[ $? -eq 0 ]]; then
echo -e "${TIMESTAMP}\t${HOST}\tOK"
else
echo -e "${TIMESTAMP}\t${HOST}\tFAIL"
EXIT_STATUS=1
fi
done
exit ${EXIT_STATUS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment