📖 Scenario: You are a network technician who needs to check if several important servers are reachable. Doing this manually every time is slow and boring. Automating this task with a script saves time and avoids mistakes.
🎯 Goal: Build a simple Bash script that checks if a list of servers are reachable by pinging them. The script will automate the connectivity check and print the results.
📋 What You'll Learn
Create a Bash array with exact server names
Create a variable for the ping count
Use a for loop to ping each server using the ping count variable
Print whether each server is reachable or not
💡 Why This Matters
🌍 Real World
Network administrators often need to check if servers or devices are online. Automating this with scripts saves time and reduces errors.
💼 Career
Knowing how to write simple network scripts is useful for IT support, system administrators, and network engineers to monitor and troubleshoot connectivity.
Progress0 / 4 steps
1
Create the server list
Create a Bash array called servers with these exact entries: "google.com", "github.com", and "example.com".
Bash Scripting
Hint
Use parentheses and quotes to create a Bash array.
2
Set the ping count
Create a variable called ping_count and set it to 2 to specify how many ping packets to send.
Bash Scripting
Hint
Just assign the number 2 to the variable ping_count.
3
Ping each server
Use a for loop with the variable server to iterate over servers. Inside the loop, ping each server using ping -c $ping_count $server and check if the ping was successful using $?.
Bash Scripting
Hint
Use the ping command with -c option and check the exit status with $?.
4
Display the results
Run the script so it prints whether each server is reachable or not. The output should show exactly these lines for each server: google.com is reachable, github.com is reachable, and example.com is reachable (or not reachable if ping fails).
Bash Scripting
Hint
Just run the script. The echo commands inside the loop print the results.
Practice
(1/5)
1. Why do network scripts automate connectivity tasks in Bash scripting?
easy
A. To replace hardware components
B. To make the computer run faster
C. To save time by automating repetitive tasks
D. To create graphical user interfaces
Solution
Step 1: Understand the purpose of network scripts
Network scripts are designed to handle repetitive connectivity tasks automatically.
Step 2: Identify the main benefit
Automating these tasks saves time and reduces manual effort.
Final Answer:
To save time by automating repetitive tasks -> Option C
Quick Check:
Automation saves time = A [OK]
Hint: Think about why automation helps daily tasks [OK]
Common Mistakes:
Confusing automation with hardware speed
Thinking scripts create GUIs
Assuming scripts replace physical devices
2. Which of the following is the correct Bash syntax to check if a server is reachable using ping?
easy
A. ping -c 3 example.com
B. ping example.com -count 3
C. ping /c 3 example.com
D. ping -count=3 example.com
Solution
Step 1: Recall the correct ping command syntax
The correct option uses '-c' to specify count of pings in Bash.
Step 2: Verify the options
Only 'ping -c 3 example.com' is valid; others use incorrect flags or syntax.
Final Answer:
ping -c 3 example.com -> Option A
Quick Check:
Correct ping flag = -c [OK]
Hint: Remember ping uses -c for count in Bash [OK]
Common Mistakes:
Using Windows ping flags in Bash
Placing flags after the hostname
Using incorrect flag names
3. What will be the output of this Bash script snippet?
if ping -c 1 8.8.8.8 >/dev/null 2>&1; then
echo "Internet is reachable"
else
echo "Internet is not reachable"
fi
medium
A. Internet is reachable
B. Internet is not reachable
C. Syntax error
D. No output
Solution
Step 1: Understand the ping command and redirection
The script pings 8.8.8.8 once and hides output by redirecting to /dev/null.
Step 2: Check the if condition
If ping succeeds, it prints "Internet is reachable"; otherwise, "Internet is not reachable".
Final Answer:
Internet is reachable -> Option A
Quick Check:
Successful ping = "Internet is reachable" [OK]
Hint: Ping 8.8.8.8 once; success means reachable [OK]
Common Mistakes:
Misunderstanding output redirection
Confusing success and failure conditions
Expecting ping output to show
4. Identify the error in this Bash script snippet that tries to restart the network service:
service network restart
if [$? -eq 0]; then
echo "Network restarted successfully"
else
echo "Failed to restart network"
fi
medium
A. Incorrect echo syntax
B. Missing space after '[' in the if condition
C. Wrong command to restart network service
D. No error, script is correct
Solution
Step 1: Check the if condition syntax
The condition uses '[$? -eq 0]' but needs spaces: '[ $? -eq 0 ]'.
Step 2: Confirm other parts
The service command and echo syntax are correct; only spacing is wrong.
Final Answer:
Missing space after '[' in the if condition -> Option B
Quick Check:
Spaces needed in [ ] test = C [OK]
Hint: Always put spaces inside [ ] in Bash conditions [OK]
Common Mistakes:
Omitting spaces inside [ ]
Using wrong service command
Miswriting echo statements
5. You want to automate checking multiple servers' connectivity and log results. Which Bash script approach is best?
hard
A. Restart network service repeatedly without checking servers
B. Manually ping each server one by one and write results by hand
C. Use a graphical tool instead of scripting
D. Use a loop to ping each server and append results to a log file
Solution
Step 1: Understand the task requirements
You need to check multiple servers automatically and save results.
Step 2: Choose the best scripting method
A loop that pings each server and logs output automates the task efficiently.
Final Answer:
Use a loop to ping each server and append results to a log file -> Option D
Quick Check:
Loop + logging = automation [OK]
Hint: Loops automate repeated tasks; logs save results [OK]
Common Mistakes:
Doing manual checks instead of scripting
Ignoring logging of results
Restarting services unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.