Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Port scanning basics in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Port Scanning Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this simple port scan script?
Consider this bash script that scans ports 80 and 443 on localhost. What output will it produce?
Bash Scripting
for port in 80 443; do
  timeout 1 bash -c "</dev/tcp/127.0.0.1/$port" && echo "Port $port is open" || echo "Port $port is closed"
done
APort 80 is closed\nPort 443 is open
BPort 80 is closed\nPort 443 is closed
CPort 80 is open\nPort 443 is closed
DPort 80 is open\nPort 443 is open
Attempts:
2 left
💡 Hint
Think about which common services usually listen on ports 80 and 443 on localhost.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this port scanning snippet
Which option contains the correct syntax to scan port 22 on 192.168.1.1 using bash's /dev/tcp?
Bash Scripting
timeout 1 bash -c "</dev/tcp/192.168.1.1/22" && echo "Open" || echo "Closed"
Atimeout 1 bash -c "</dev/tcp/192.168.1.1/22" && echo "Open" || echo "Closed"
Btimeout 1 bash -c "</dev/tcp/192.168.1.1:22" && echo "Open" || echo "Closed"
Ctimeout 1 bash -c "</dev/tcp/192.168.1.1" 22 && echo "Open" || echo "Closed"
Dtimeout 1 bash -c "</dev/tcp/192.168.1.1-22" && echo "Open" || echo "Closed"
Attempts:
2 left
💡 Hint
Check the correct format for /dev/tcp host and port separation.
🔧 Debug
advanced
2:00remaining
Why does this port scan script always report ports as closed?
This script tries to scan ports 20 to 22 on 10.0.0.1 but always prints 'Port X is closed'. What is the likely cause?
Bash Scripting
for port in {20..22}; do
  if timeout 1 bash -c "</dev/tcp/10.0.0.1/$port"; then
    echo "Port $port is open"
  else
    echo "Port $port is closed"
  fi
done
AThe target host 10.0.0.1 is unreachable or blocking connections
BThe timeout command is not installed, so the script fails silently
CThe bash shell does not support /dev/tcp on this system
DThe for loop syntax {20..22} is invalid in bash
Attempts:
2 left
💡 Hint
Check network connectivity and firewall rules on the target host.
🚀 Application
advanced
2:00remaining
How to scan multiple ports efficiently in bash?
You want to scan ports 22, 80, 443, and 8080 on host 192.168.0.10 using bash. Which script snippet scans all ports and prints only open ones?
Afor p in 22 80 443 8080; do timeout 1 bash -c "</dev/tcp/192.168.0.10/$p" && echo "Port $p open"; done
Bfor p in 22 80 443 8080; do timeout 1 bash -c "</dev/tcp/192.168.0.10/$p" || echo "Port $p open"; done
Cfor p in 22 80 443 8080; do if timeout 1 bash -c "</dev/tcp/192.168.0.10/$p"; then echo "Port $p open"; fi; done
Dfor p in 22 80 443 8080; do if ! timeout 1 bash -c "</dev/tcp/192.168.0.10/$p"; then echo "Port $p open"; fi; done
Attempts:
2 left
💡 Hint
Only print when the connection succeeds, not when it fails.
🧠 Conceptual
expert
2:00remaining
What is a limitation of using bash /dev/tcp for port scanning?
Which of the following is a true limitation when using bash's /dev/tcp feature for port scanning?
AIt provides detailed service banners for each open port
BIt cannot scan UDP ports because /dev/tcp only supports TCP connections
CIt automatically scans all ports from 1 to 65535 without specifying ports
DIt requires root privileges to open /dev/tcp sockets
Attempts:
2 left
💡 Hint
Think about the protocol support of /dev/tcp.

Practice

(1/5)
1. What is the main purpose of port scanning in network security?
easy
A. To install software remotely
B. To delete files on a remote server
C. To speed up internet connection
D. To find open ports on a computer

Solution

  1. Step 1: Understand port scanning basics

    Port scanning is used to check which network ports on a computer are open and accepting connections.
  2. Step 2: Identify the correct purpose

    Among the options, only finding open ports matches the purpose of port scanning.
  3. Final Answer:

    To find open ports on a computer -> Option D
  4. Quick Check:

    Port scanning = find open ports [OK]
Hint: Port scanning checks open ports, not files or speed [OK]
Common Mistakes:
  • Confusing port scanning with file operations
  • Thinking it improves internet speed
  • Assuming it installs software
2. Which of the following bash commands correctly scans if port 80 is open on host 192.168.1.1?
easy
A. nc -zv 192.168.1.1 80
B. nc -xv 192.168.1.1 80
C. nc -z 192.168.1.1 -p 80
D. nc -v 192.168.1.1

Solution

  1. Step 1: Recall correct netcat syntax for port scanning

    The option '-z' tells nc to scan without sending data, and '-v' enables verbose output. The host and port follow these flags.
  2. Step 2: Check each option

    Only nc -zv 192.168.1.1 80 uses the correct flags '-z' for scanning and '-v' for verbose, followed by host and port. Other options either lack necessary flags, use incorrect flags like -xv or misuse -p for source port, or omit the port specification.
  3. Final Answer:

    nc -zv 192.168.1.1 80 -> Option A
  4. Quick Check:

    Correct nc flags = -zv [OK]
Hint: Use nc -zv host port to scan a port [OK]
Common Mistakes:
  • Using wrong flags like -xv
  • Misplacing port with -p flag
  • Omitting port number
3. What will be the output of this bash snippet scanning ports 22 and 80 on localhost?
for port in 22 80; do
  nc -zv localhost $port
 done
medium
A. It will produce a syntax error
B. It will show if ports 22 and 80 are open or closed on localhost
C. It will scan ports 22 and 80 on a remote host
D. It will scan all ports from 22 to 80

Solution

  1. Step 1: Analyze the for loop and command

    The loop iterates over ports 22 and 80, running 'nc -zv localhost $port' for each port.
  2. Step 2: Understand the command effect

    Netcat checks if each specified port on localhost is open, printing the result.
  3. Final Answer:

    It will show if ports 22 and 80 are open or closed on localhost -> Option B
  4. Quick Check:

    Loop scans specified ports on localhost [OK]
Hint: Loop scans listed ports on localhost, not a range [OK]
Common Mistakes:
  • Thinking it scans a range of ports
  • Assuming remote host scanning
  • Expecting syntax errors
4. Identify the error in this bash script intended to scan ports 20 to 25 on 127.0.0.1:
for port in {20..25} do
  nc -zv 127.0.0.1 $port
 done
medium
A. The loop syntax is incorrect; it should use parentheses
B. The loop variable $port is not referenced correctly
C. The script is missing a semicolon after the for statement
D. The loop block is missing a 'do' keyword

Solution

  1. Step 1: Check for correct bash for-loop syntax

    Bash requires a semicolon or newline after the for statement before 'do'. Here, 'for port in {20..25}' is not followed by a semicolon or newline before 'do'.
  2. Step 2: Verify other syntax elements

    The loop variable and block are correct, and 'do' is present. The missing semicolon causes a syntax error.
  3. Final Answer:

    The script is missing a semicolon after the for statement -> Option C
  4. Quick Check:

    Missing semicolon after for statement causes error [OK]
Hint: Add semicolon or newline after for statement before do [OK]
Common Mistakes:
  • Omitting semicolon after for line
  • Misplacing 'do' keyword
  • Incorrect variable usage
5. You want to scan all ports from 80 to 85 on host 10.0.0.5 and save only open ports to a file named open_ports.txt. Which bash script snippet achieves this?
hard
A. for port in {80..85}; do nc -zv 10.0.0.5 $port 2>&1 | grep 'succeeded' >> open_ports.txt; done
B. for port in 80 81 82 83 84 85; do nc -zv 10.0.0.5 $port > open_ports.txt; done
C. nc -zv 10.0.0.5 80-85 > open_ports.txt
D. for port in {80..85}; do nc -zv 10.0.0.5 $port | grep 'failed' >> open_ports.txt; done

Solution

  1. Step 1: Understand the scanning and filtering requirements

    The script must scan ports 80 to 85, detect open ports, and save only those results to a file.
  2. Step 2: Analyze each option

    for port in {80..85}; do nc -zv 10.0.0.5 $port 2>&1 | grep 'succeeded' >> open_ports.txt; done loops over ports 80 to 85, runs nc with verbose and zero-I/O flags, redirects stderr to stdout, filters lines containing 'succeeded' (indicating open ports), and appends to open_ports.txt. for port in 80 81 82 83 84 85; do nc -zv 10.0.0.5 $port > open_ports.txt; done overwrites the file each time and does not filter open ports. nc -zv 10.0.0.5 80-85 > open_ports.txt uses invalid port range syntax. for port in {80..85}; do nc -zv 10.0.0.5 $port | grep 'failed' >> open_ports.txt; done filters 'failed' ports, opposite of requirement.
  3. Final Answer:

    for port in {80..85}; do nc -zv 10.0.0.5 $port 2>&1 | grep 'succeeded' >> open_ports.txt; done -> Option A
  4. Quick Check:

    Loop + grep 'succeeded' + append to file = correct [OK]
Hint: Filter 'succeeded' lines and append output to file [OK]
Common Mistakes:
  • Overwriting output file inside loop
  • Using invalid port range syntax
  • Filtering 'failed' instead of 'succeeded'
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