nerdexam
CompTIA

PT0-003 · Question #93

A penetration tester wants to use the following Bash script to identify active servers on a network: 1 network_addr="192.168.1" 2 for h in {1..254}; do 3 ping -c 1 -W 1 $network_addr.$h > /dev/null…

The correct answer is C. Use seq on the loop on line 2. The provided Bash script is used to ping a range of IP addresses to identify active hosts in a Original Script: 1 network_addr="192.168.1" 2 for h in {1..254}; do 3 ping -c 1 -W 1 $network_addr.$h > /dev/null 4 if [ $? -eq 0 ]; then 5 echo "Host $h is up" 7 echo "Host $h is…

Submitted by wei.xz· Mar 6, 2026Reconnaissance and Enumeration

Question

A penetration tester wants to use the following Bash script to identify active servers on a network:

1 network_addr="192.168.1" 2 for h in {1..254}; do 3 ping -c 1 -W 1 $network_addr.$h > /dev/null 4 if [ $? -eq 0 ]; then 5 echo "Host $h is up" 6 else 7 echo "Host $h is down" 8 fi 9 done Which of the following should the tester do to modify the script?

Options

  • AChange the condition on line 4.
  • BAdd 2>&1 at the end of line 3.
  • CUse seq on the loop on line 2.
  • DReplace $h with ${h} on line 3.

How the community answered

(20 responses)
  • A
    5% (1)
  • B
    15% (3)
  • C
    70% (14)
  • D
    10% (2)

Explanation

The provided Bash script is used to ping a range of IP addresses to identify active hosts in a Original Script: 1 network_addr="192.168.1" 2 for h in {1..254}; do 3 ping -c 1 -W 1 $network_addr.$h > /dev/null 4 if [ $? -eq 0 ]; then 5 echo "Host $h is up" 7 echo "Host $h is down" Line 2: The loop uses {1..254} to iterate over the range of host addresses. However, this notation might not work in all shell environments, especially if not using bash directly or if the script runs in a different shell. Using seq for Better Compatibility: The seq command is a more compatible way to generate a sequence of numbers. It ensures the loop works in any POSIX-compliant shell. Modified Line 2: for h in $(seq 1 254); do This change ensures broader compatibility and reliability of the script. Modified Script: 1 network_addr="192.168.1" 2 for h in $(seq 1 254); do 3 ping -c 1 -W 1 $network_addr.$h > /dev/null 4 if [ $? -eq 0 ]; then 5 echo "Host $h is up" 7 echo "Host $h is down"

Topics

#Bash scripting#network scanning#scripting error

Community Discussion

No community discussion yet for this question.

Full PT0-003 Practice