0
0
Bash Scriptingscripting~10 mins

Network monitoring script in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Bash Scripting
#!/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
This script continuously pings the target IP 8.8.8.8 every 5 seconds and prints if the host is up or down.
Execution Table
StepActionPing Command ResultCondition CheckOutputNext Step
1Ping 8.8.8.8 onceSuccess (exit code 0)0 == 0Host is upSleep 5 seconds
2Wait 5 secondsN/AN/AN/ARepeat ping
3Ping 8.8.8.8 onceFail (exit code 1)1 == 0 is falseHost is downSleep 5 seconds
4Wait 5 secondsN/AN/AN/ARepeat ping
...Loop continues............
ExitUser stops scriptN/AN/AScript endsStop
💡 Script runs indefinitely until user stops it manually.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
TARGET8.8.8.88.8.8.88.8.8.88.8.8.8
Ping exit codeN/A0 (success)1 (fail)Varies each ping
Key Moments - 3 Insights
Why does the script use 'ping -c 1' instead of just 'ping'?
Using 'ping -c 1' sends only one ping packet per check, so the script can quickly determine if the host is reachable without waiting for multiple pings. This is shown in execution_table step 1.
What does the '$?' variable represent in the script?
'$?' holds the exit code of the last command, here the ping. A zero means success (host reachable), non-zero means failure. This is checked in the condition at step 1 and 3 in the execution_table.
Why is there a 'sleep 5' command after each ping?
The 'sleep 5' pauses the script for 5 seconds to avoid flooding the network with pings and to space out checks. This is the 'Wait 5 seconds' action in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when the ping command exit code is 0?
AHost is up
BHost is down
CPing failed
DNo output
💡 Hint
Check the 'Output' column in step 1 of the execution_table where ping exit code is 0.
At which step does the script wait before repeating the ping?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Wait 5 seconds' in the 'Action' column of the execution_table.
If the ping command always fails, what will be the output at step 3?
AHost is up
BHost is down
CNo output
DScript exits
💡 Hint
Refer to step 3 in execution_table where ping fails and output is shown.
Concept Snapshot
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
Full Transcript
This network monitoring script uses a loop to ping a target IP address once every 5 seconds. It checks the exit code of the ping command to determine if the host is reachable. If the exit code is zero, it prints 'Host is up'; otherwise, it prints 'Host is down'. The script then waits 5 seconds before repeating the check. This cycle continues indefinitely until the user stops the script manually.