Concept Flow - Network monitoring script
Start script
Ping target IP
Check ping result
Log success
Wait for interval
Repeat or Exit
The script pings a target IP, checks if it is reachable, logs the result, waits for a set time, and repeats.
#!/bin/bash TARGET=8.8.8.8 while true; do ping -c 1 $TARGET > /dev/null if [ $? -eq 0 ]; then echo "Host is up" else echo "Host is down" fi sleep 5 done
| Step | Action | Ping Command Result | Condition Check | Output | Next Step |
|---|---|---|---|---|---|
| 1 | Ping 8.8.8.8 once | Success (exit code 0) | 0 == 0 | Host is up | Sleep 5 seconds |
| 2 | Wait 5 seconds | N/A | N/A | N/A | Repeat ping |
| 3 | Ping 8.8.8.8 once | Fail (exit code 1) | 1 == 0 is false | Host is down | Sleep 5 seconds |
| 4 | Wait 5 seconds | N/A | N/A | N/A | Repeat ping |
| ... | Loop continues | ... | ... | ... | ... |
| Exit | User stops script | N/A | N/A | Script ends | Stop |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| TARGET | 8.8.8.8 | 8.8.8.8 | 8.8.8.8 | 8.8.8.8 |
| Ping exit code | N/A | 0 (success) | 1 (fail) | Varies each ping |
Network monitoring script in bash: - Use 'ping -c 1 <IP>' to send one ping - Check exit code with '$?' - If 0, host is reachable; else unreachable - Log result with echo - Use 'sleep <seconds>' to wait between checks - Loop with 'while true' for continuous monitoring
ping?pingping sends a small message to a device to see if it replies, showing if it is reachable.ping to detect if devices respond, helping find network problems early.-c specifies the number of ping requests to send.ping -c 1 192.168.1.1 sends exactly one ping to the IP address.if ping -c 1 8.8.8.8 > /dev/null; then echo "Host is reachable" else echo "Host is unreachable" fi
ping -c 1 8.8.8.8 sends one ping to Google's DNS server, which is usually reachable.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
for ip in ... but lacks the closing done keyword.unreachable.log. Which snippet correctly does this?unreachable.log.ping -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.Pass the test and this pattern moves unknown