0
0
Bash Scriptingscripting~20 mins

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

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