Skip to content

Instantly share code, notes, and snippets.

@Jachimo
Created May 5, 2025 16:04
Show Gist options
  • Save Jachimo/6aa7f41b44200ce2d87634cdc8a35b62 to your computer and use it in GitHub Desktop.
Save Jachimo/6aa7f41b44200ce2d87634cdc8a35b62 to your computer and use it in GitHub Desktop.
Shell script to ping a list of IP addresses provided in a text file
#!/bin/bash
# Usage: ./ping_list.sh FILE
# FILE is a text file containing IP addresses, one per line, to be pinged
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <file_with_ip_addresses>"
exit 1
fi
FILE="$1" # filename
if [ ! -f "$FILE" ]; then
echo "Error: File '$FILE' not found!"
exit 1
fi
# Arrays to store results
reachable=()
unreachable=()
while IFS= read -r ip_address; do
# Ignore empty lines and lines starting with '#'
if [[ -n "$ip_address" && "$ip_address" != \#* ]]; then
echo "Pinging $ip_address..."
if ping -c 5 -q "$ip_address" > /dev/null 2>&1; then
echo "$ip_address is reachable."
reachable+=("$ip_address")
else
echo "$ip_address is not reachable."
unreachable+=("$ip_address")
fi
echo "---------------------------------"
fi
done < "$FILE"
echo "Summary:"
echo "Reachable IP addresses:"
for ip in "${reachable[@]}"; do
echo "- $ip"
done
echo ""
echo "Unreachable IP addresses:"
for ip in "${unreachable[@]}"; do
echo "- $ip"
done
echo "Finished pinging all IP addresses."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment