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