Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Why network scripts automate connectivity tasks in Bash Scripting - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the network is reachable by pinging google.com once.

Bash Scripting
ping -c 1 [1]
Drag options to blanks, or click blank then click option'
Aexample.com
Blocalhost
C192.168.1.1
Dgoogle.com
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'localhost' will only test your own machine, not the network.
Using an IP like 192.168.1.1 might not be reachable depending on your network.
2fill in blank
medium

Complete the script to store the IP address of the default gateway in the variable gateway_ip.

Bash Scripting
gateway_ip=$(ip route | grep default | [1] '{print $3}')
Drag options to blanks, or click blank then click option'
Acut
Bawk
Csed
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cut' without specifying delimiter and field might not work as expected.
Using 'sed' is more complex for this simple extraction.
3fill in blank
hard

Fix the error in the script to correctly check if the network interface eth0 is up.

Bash Scripting
if ip link show [1] | grep -q 'state UP'; then
  echo "Interface is up"
else
  echo "Interface is down"
fi
Drag options to blanks, or click blank then click option'
Aeth1
Blo
Ceth0
Dwlan0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lo' checks the loopback interface, which is always up.
Using 'wlan0' checks wireless interface, which may not exist.
4fill in blank
hard

Fill both blanks to create a script that lists all active network interfaces and their IP addresses.

Bash Scripting
ip -o [2] [1] show | awk '{print $2, $4}'
Drag options to blanks, or click blank then click option'
Aaddr
B-4
C-6
Dlink
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-6' lists IPv6 addresses, not IPv4.
Using 'link' does not show IP addresses.
5fill in blank
hard

Fill all three blanks to create a dictionary-like output of interface names and their IPv4 addresses, excluding interfaces without IP.

Bash Scripting
declare -A ip_map
while read -r [1] [2]; do
  ip_map[[1]]=[2]
done < <(ip -o -4 addr show | awk '{print $2, $4}' | cut -d/ -f1)
Drag options to blanks, or click blank then click option'
Aiface
Bip_addr
Cip
Dinterface
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both interface and IP causes errors.
Not cutting the IP address to remove subnet mask causes wrong values.

Practice

(1/5)
1. Why do network scripts automate connectivity tasks in Bash scripting?
easy
A. To replace hardware components
B. To make the computer run faster
C. To save time by automating repetitive tasks
D. To create graphical user interfaces

Solution

  1. Step 1: Understand the purpose of network scripts

    Network scripts are designed to handle repetitive connectivity tasks automatically.
  2. Step 2: Identify the main benefit

    Automating these tasks saves time and reduces manual effort.
  3. Final Answer:

    To save time by automating repetitive tasks -> Option C
  4. Quick Check:

    Automation saves time = A [OK]
Hint: Think about why automation helps daily tasks [OK]
Common Mistakes:
  • Confusing automation with hardware speed
  • Thinking scripts create GUIs
  • Assuming scripts replace physical devices
2. Which of the following is the correct Bash syntax to check if a server is reachable using ping?
easy
A. ping -c 3 example.com
B. ping example.com -count 3
C. ping /c 3 example.com
D. ping -count=3 example.com

Solution

  1. Step 1: Recall the correct ping command syntax

    The correct option uses '-c' to specify count of pings in Bash.
  2. Step 2: Verify the options

    Only 'ping -c 3 example.com' is valid; others use incorrect flags or syntax.
  3. Final Answer:

    ping -c 3 example.com -> Option A
  4. Quick Check:

    Correct ping flag = -c [OK]
Hint: Remember ping uses -c for count in Bash [OK]
Common Mistakes:
  • Using Windows ping flags in Bash
  • Placing flags after the hostname
  • Using incorrect flag names
3. What will be the output of this Bash script snippet?
if ping -c 1 8.8.8.8 >/dev/null 2>&1; then
  echo "Internet is reachable"
else
  echo "Internet is not reachable"
fi
medium
A. Internet is reachable
B. Internet is not reachable
C. Syntax error
D. No output

Solution

  1. Step 1: Understand the ping command and redirection

    The script pings 8.8.8.8 once and hides output by redirecting to /dev/null.
  2. Step 2: Check the if condition

    If ping succeeds, it prints "Internet is reachable"; otherwise, "Internet is not reachable".
  3. Final Answer:

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

    Successful ping = "Internet is reachable" [OK]
Hint: Ping 8.8.8.8 once; success means reachable [OK]
Common Mistakes:
  • Misunderstanding output redirection
  • Confusing success and failure conditions
  • Expecting ping output to show
4. Identify the error in this Bash script snippet that tries to restart the network service:
service network restart
if [$? -eq 0]; then
  echo "Network restarted successfully"
else
  echo "Failed to restart network"
fi
medium
A. Incorrect echo syntax
B. Missing space after '[' in the if condition
C. Wrong command to restart network service
D. No error, script is correct

Solution

  1. Step 1: Check the if condition syntax

    The condition uses '[$? -eq 0]' but needs spaces: '[ $? -eq 0 ]'.
  2. Step 2: Confirm other parts

    The service command and echo syntax are correct; only spacing is wrong.
  3. Final Answer:

    Missing space after '[' in the if condition -> Option B
  4. Quick Check:

    Spaces needed in [ ] test = C [OK]
Hint: Always put spaces inside [ ] in Bash conditions [OK]
Common Mistakes:
  • Omitting spaces inside [ ]
  • Using wrong service command
  • Miswriting echo statements
5. You want to automate checking multiple servers' connectivity and log results. Which Bash script approach is best?
hard
A. Restart network service repeatedly without checking servers
B. Manually ping each server one by one and write results by hand
C. Use a graphical tool instead of scripting
D. Use a loop to ping each server and append results to a log file

Solution

  1. Step 1: Understand the task requirements

    You need to check multiple servers automatically and save results.
  2. Step 2: Choose the best scripting method

    A loop that pings each server and logs output automates the task efficiently.
  3. Final Answer:

    Use a loop to ping each server and append results to a log file -> Option D
  4. Quick Check:

    Loop + logging = automation [OK]
Hint: Loops automate repeated tasks; logs save results [OK]
Common Mistakes:
  • Doing manual checks instead of scripting
  • Ignoring logging of results
  • Restarting services unnecessarily
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