Skip to content

Instantly share code, notes, and snippets.

@foull
Last active February 29, 2024 22:00
Show Gist options
  • Save foull/8595631f6856220fca04d288d721f694 to your computer and use it in GitHub Desktop.
Save foull/8595631f6856220fca04d288d721f694 to your computer and use it in GitHub Desktop.
ping_servers.sh
#!/bin/bash
# Ensure at least one server is provided
if [ "$#" -eq 0 ]; then
echo "Please provide at least one server as an argument."
exit 1
fi
# Loop through all provided servers
for server in "$@"
do
# Ping the server, get the line with "avg" in it, then use awk to print the 4th field (avg ping)
ping_result=$(ping -c 10 "$server" | grep avg | awk -F '/' '{print $5}')
if [ -z "$ping_result" ]; then
echo "Could not retrieve ping for $server"
else
echo "[$server] [$ping_result ms]"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment