Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a network monitoring script using ping?
easy
A. To increase the speed of the network
B. To install software on remote devices
C. To change the IP addresses of devices
D. To check if devices on the network are reachable

Solution

  1. Step 1: Understand the role of ping

    ping sends a small message to a device to see if it replies, showing if it is reachable.
  2. Step 2: Identify the script's goal

    The script uses ping to detect if devices respond, helping find network problems early.
  3. Final Answer:

    To check if devices on the network are reachable -> Option D
  4. Quick Check:

    Ping checks reachability = B [OK]
Hint: Ping tests if a device replies to confirm reachability [OK]
Common Mistakes:
  • Confusing ping with speed testing
  • Thinking ping changes device settings
  • Assuming ping installs software
2. Which of the following is the correct syntax to ping a device once in a bash script?
easy
A. ping /c 1 192.168.1.1
B. ping -n 1 192.168.1.1
C. ping -c 1 192.168.1.1
D. ping 192.168.1.1 -count 1

Solution

  1. Step 1: Recall the ping option for count

    In Linux bash, -c specifies the number of ping requests to send.
  2. Step 2: Verify the correct command format

    ping -c 1 192.168.1.1 sends exactly one ping to the IP address.
  3. Final Answer:

    ping -c 1 192.168.1.1 -> Option C
  4. Quick Check:

    Use -c for count in ping = D [OK]
Hint: Use -c to specify ping count in bash [OK]
Common Mistakes:
  • Using Windows ping options like -n in bash
  • Incorrect option syntax like /c or -count
  • Omitting the count option for single ping
3. What will be the output of this script snippet?
if ping -c 1 8.8.8.8 > /dev/null; then
  echo "Host is reachable"
else
  echo "Host is unreachable"
fi
medium
A. Host is reachable
B. Syntax error
C. Host is unreachable
D. No output

Solution

  1. Step 1: Understand the ping command behavior

    The command ping -c 1 8.8.8.8 sends one ping to Google's DNS server, which is usually reachable.
  2. Step 2: Analyze the if condition and output

    If ping succeeds, the script prints "Host is reachable"; otherwise, it prints "Host is unreachable".
  3. Final Answer:

    Host is reachable -> Option A
  4. Quick Check:

    Successful ping prints reachable = C [OK]
Hint: Ping 8.8.8.8 usually succeeds, so output is reachable [OK]
Common Mistakes:
  • Assuming ping always fails
  • Confusing output redirection with errors
  • Missing that output is suppressed with > /dev/null
4. Identify the error in this script snippet for monitoring a device:
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
medium
A. Incorrect ping option '-c'
B. Missing 'done' to close the for loop
C. Wrong variable name for IP address
D. No error, script is correct

Solution

  1. Step 1: Check for loop syntax completeness

    The for loop starts with for ip in ... but lacks the closing done keyword.
  2. Step 2: Confirm other parts are correct

    The ping command and if condition use correct syntax and variable names.
  3. Final Answer:

    Missing 'done' to close the for loop -> Option B
  4. Quick Check:

    For loops need 'done' to end = A [OK]
Hint: Always end bash loops with 'done' [OK]
Common Mistakes:
  • Forgetting 'done' after loops
  • Using wrong ping options
  • Using undefined variables
5. You want to create a script that checks multiple IPs and logs only unreachable ones to a file named unreachable.log. Which snippet correctly does this?
hard
A. for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip > /dev/null || echo "$ip unreachable" >> unreachable.log done
B. 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
C. for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip >> unreachable.log done
D. for ip in 192.168.1.1 192.168.1.2; do ping -c 1 $ip || echo "$ip reachable" >> unreachable.log done

Solution

  1. Step 1: Understand the goal

    The script should log only unreachable IPs to unreachable.log.
  2. 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 uses 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.
  3. 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 A
  4. Quick Check:

    Use '||' to log failures only = A [OK]
Hint: Use '||' to log only when ping fails [OK]
Common Mistakes:
  • Logging reachable IPs instead of unreachable
  • Appending all ping output to log
  • Using wrong conditional logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard