Network monitoring script in Bash Scripting - Time & Space Complexity
We want to understand how the time needed to run a network monitoring script changes as we check more hosts.
How does the script's work grow when the list of hosts gets bigger?
Analyze the time complexity of the following code snippet.
#!/bin/bash
hosts=("192.168.1.1" "192.168.1.2" "192.168.1.3")
for host in "${hosts[@]}"; do
ping -c 1 "$host" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$host is up"
else
echo "$host is down"
fi
done
This script checks if each host in the list is reachable by sending one ping and then prints if it is up or down.
- Primary operation: The for-loop that pings each host once.
- How many times: Once for each host in the list.
Each new host adds one more ping command to run, so the total work grows directly with the number of hosts.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pings |
| 100 | 100 pings |
| 1000 | 1000 pings |
Pattern observation: The work increases steadily and proportionally as the list of hosts grows.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of hosts checked.
[X] Wrong: "The script runs in constant time because each ping is quick."
[OK] Correct: Even if one ping is fast, the script runs one ping per host, so more hosts mean more total time.
Understanding how loops affect time helps you explain script efficiency clearly and shows you can think about scaling tasks.
"What if we changed the script to ping each host 5 times instead of once? How would the time complexity change?"
Practice
ping?Solution
Step 1: Understand the role of
pingpingsends a small message to a device to see if it replies, showing if it is reachable.Step 2: Identify the script's goal
The script usespingto detect if devices respond, helping find network problems early.Final Answer:
To check if devices on the network are reachable -> Option DQuick Check:
Ping checks reachability = B [OK]
- Confusing ping with speed testing
- Thinking ping changes device settings
- Assuming ping installs software
Solution
Step 1: Recall the ping option for count
In Linux bash,-cspecifies the number of ping requests to send.Step 2: Verify the correct command format
ping -c 1 192.168.1.1sends exactly one ping to the IP address.Final Answer:
ping -c 1 192.168.1.1 -> Option CQuick Check:
Use -c for count in ping = D [OK]
- Using Windows ping options like -n in bash
- Incorrect option syntax like /c or -count
- Omitting the count option for single ping
if ping -c 1 8.8.8.8 > /dev/null; then echo "Host is reachable" else echo "Host is unreachable" fi
Solution
Step 1: Understand the ping command behavior
The commandping -c 1 8.8.8.8sends one ping to Google's DNS server, which is usually reachable.Step 2: Analyze the if condition and output
If ping succeeds, the script prints "Host is reachable"; otherwise, it prints "Host is unreachable".Final Answer:
Host is reachable -> Option AQuick Check:
Successful ping prints reachable = C [OK]
- Assuming ping always fails
- Confusing output redirection with errors
- Missing that output is suppressed with > /dev/null
for ip in 192.168.1.1 192.168.1.2
do
ping -c 1 $ip
if [ $? -eq 0 ]; then
echo "$ip is up"
else
echo "$ip is down"
fi
Solution
Step 1: Check for loop syntax completeness
The for loop starts withfor ip in ...but lacks the closingdonekeyword.Step 2: Confirm other parts are correct
The ping command and if condition use correct syntax and variable names.Final Answer:
Missing 'done' to close the for loop -> Option BQuick Check:
For loops need 'done' to end = A [OK]
- Forgetting 'done' after loops
- Using wrong ping options
- Using undefined variables
unreachable.log. Which snippet correctly does this?Solution
Step 1: Understand the goal
The script should log only unreachable IPs tounreachable.log.Step 2: Analyze each option
for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip > /dev/null || echo "$ip unreachable" >> unreachable.log done usesping -c 1 $ip > /dev/null || echo "$ip unreachable" >> unreachable.log, which logs IPs only if ping fails (unreachable). for ip in 192.168.1.1 192.168.1.2; do if ping -c 1 $ip > /dev/null; then echo "$ip unreachable" >> unreachable.log fi done logs reachable IPs incorrectly. for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip >> unreachable.log done logs all ping output, not just unreachable. for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip || echo "$ip reachable" >> unreachable.log done logs reachable IPs as unreachable.Final Answer:
for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip > /dev/null || echo "$ip unreachable" >> unreachable.log done -> Option AQuick Check:
Use '||' to log failures only = A [OK]
- Logging reachable IPs instead of unreachable
- Appending all ping output to log
- Using wrong conditional logic
Ready to prove it?
Pass the test and this pattern moves unknown
