0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Ping Multiple Hosts with Output

Use a Bash script with a loop like for host in host1 host2; do ping -c 1 -W 1 $host && echo "$host is reachable" || echo "$host is unreachable"; done to ping multiple hosts and show their status.
📋

Examples

Inputhosts="google.com github.com"
Outputgoogle.com is reachable github.com is reachable
Inputhosts="192.168.1.1 10.255.255.1"
Output192.168.1.1 is reachable 10.255.255.1 is unreachable
Inputhosts="localhost invalid.host"
Outputlocalhost is reachable invalid.host is unreachable
🧠

How to Think About It

To ping multiple hosts, list their names or IPs, then loop over each one. For each host, run the ping command once and check if it succeeds. Print a message saying if the host is reachable or not.
📐

Algorithm

1
Get the list of hosts to ping
2
For each host in the list, do:
3
Run ping command once to check connectivity
4
If ping succeeds, print host is reachable
5
Otherwise, print host is unreachable
💻

Code

bash
#!/bin/bash
hosts="google.com github.com invalid.host"

for host in $hosts; do
  if ping -c 1 -W 1 $host > /dev/null 2>&1; then
    echo "$host is reachable"
  else
    echo "$host is unreachable"
  fi
done
Output
google.com is reachable github.com is reachable invalid.host is unreachable
🔍

Dry Run

Let's trace pinging 'google.com' and 'invalid.host' through the script

1

Set hosts variable

hosts="google.com invalid.host"

2

Start loop with host=google.com

ping -c 1 -W 1 google.com runs and succeeds

3

Print result for google.com

Output: google.com is reachable

4

Next loop with host=invalid.host

ping -c 1 -W 1 invalid.host runs and fails

5

Print result for invalid.host

Output: invalid.host is unreachable

HostPing ResultOutput
google.comSuccessgoogle.com is reachable
invalid.hostFailinvalid.host is unreachable
💡

Why This Works

Step 1: Loop over hosts

The script uses a for loop to go through each host in the list one by one.

Step 2: Ping each host once

It runs ping -c 1 -W 1 to send one ping and wait 1 second for a reply, checking if the host is reachable.

Step 3: Check ping success

If ping returns success, the script prints the host is reachable; otherwise, it prints unreachable.

🔄

Alternative Approaches

Using a file with hosts
bash
#!/bin/bash
while read -r host; do
  if ping -c 1 -W 1 "$host" > /dev/null 2>&1; then
    echo "$host is reachable"
  else
    echo "$host is unreachable"
  fi
done < hosts.txt
Reads hosts from a file line by line, useful for many hosts but requires a file.
Parallel ping with background jobs
bash
#!/bin/bash
hosts="google.com github.com invalid.host"
for host in $hosts; do
  (ping -c 1 -W 1 $host > /dev/null 2>&1 && echo "$host is reachable" || echo "$host is unreachable") &
done
wait
Pings hosts in parallel to speed up, but output order may vary.
Using fping tool
bash
fping -c1 -t300 google.com github.com invalid.host
fping is a specialized tool for pinging multiple hosts efficiently but may need installation.

Complexity: O(n) time, O(1) space

Time Complexity

The script pings each host once, so time grows linearly with the number of hosts.

Space Complexity

The script uses constant extra memory regardless of the number of hosts.

Which Approach is Fastest?

Parallel pinging runs faster but is more complex; sequential pinging is simpler and easier to understand.

ApproachTimeSpaceBest For
Sequential ping loopO(n)O(1)Simple scripts with few hosts
Parallel ping with background jobsO(1) to O(n) depending on systemO(n)Speeding up many hosts
fping toolO(n)O(1)Efficient multi-host pinging with external tool
💡
Use -W 1 with ping to limit wait time and speed up the script.
⚠️
Forgetting to redirect ping output to /dev/null causes cluttered output.