Port scanning basics in Bash Scripting - Time & Space Complexity
When we scan ports using a script, we want to know how long it takes as the number of ports grows.
We ask: How does the time to scan change when we check more ports?
Analyze the time complexity of the following code snippet.
for port in {1..1000}; do
timeout 1 bash -c ">> /dev/tcp/127.0.0.1/$port" 2>/dev/null
if [ $? -eq 0 ]; then
echo "Port $port is open"
fi
done
This script tries to connect to each port from 1 to 1000 on localhost to see if it is open.
- Primary operation: The for-loop that tries to connect to each port one by one.
- How many times: It runs once for every port in the range (e.g., 1000 times).
Each port check takes about the same time, so total time grows as we add more ports.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 connection attempts |
| 100 | 100 connection attempts |
| 1000 | 1000 connection attempts |
Pattern observation: The time grows directly with the number of ports checked.
Time Complexity: O(n)
This means if you double the number of ports, the scanning time roughly doubles.
[X] Wrong: "The script checks all ports at the same time, so time stays the same no matter how many ports."
[OK] Correct: The script checks ports one after another, so more ports mean more time.
Understanding how loops affect time helps you explain script speed clearly and shows you can think about efficiency.
"What if we scanned multiple ports at the same time using background jobs? How would the time complexity change?"
Practice
Solution
Step 1: Understand port scanning basics
Port scanning is used to check which network ports on a computer are open and accepting connections.Step 2: Identify the correct purpose
Among the options, only finding open ports matches the purpose of port scanning.Final Answer:
To find open ports on a computer -> Option DQuick Check:
Port scanning = find open ports [OK]
- Confusing port scanning with file operations
- Thinking it improves internet speed
- Assuming it installs software
Solution
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.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.Final Answer:
nc -zv 192.168.1.1 80 -> Option AQuick Check:
Correct nc flags = -zv [OK]
- Using wrong flags like -xv
- Misplacing port with -p flag
- Omitting port number
for port in 22 80; do nc -zv localhost $port done
Solution
Step 1: Analyze the for loop and command
The loop iterates over ports 22 and 80, running 'nc -zv localhost $port' for each port.Step 2: Understand the command effect
Netcat checks if each specified port on localhost is open, printing the result.Final Answer:
It will show if ports 22 and 80 are open or closed on localhost -> Option BQuick Check:
Loop scans specified ports on localhost [OK]
- Thinking it scans a range of ports
- Assuming remote host scanning
- Expecting syntax errors
for port in {20..25} do
nc -zv 127.0.0.1 $port
doneSolution
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'.Step 2: Verify other syntax elements
The loop variable and block are correct, and 'do' is present. The missing semicolon causes a syntax error.Final Answer:
The script is missing a semicolon after the for statement -> Option CQuick Check:
Missing semicolon after for statement causes error [OK]
- Omitting semicolon after for line
- Misplacing 'do' keyword
- Incorrect variable usage
Solution
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.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.Final Answer:
for port in {80..85}; do nc -zv 10.0.0.5 $port 2>&1 | grep 'succeeded' >> open_ports.txt; done -> Option AQuick Check:
Loop + grep 'succeeded' + append to file = correct [OK]
- Overwriting output file inside loop
- Using invalid port range syntax
- Filtering 'failed' instead of 'succeeded'
Ready to prove it?
Pass the test and this pattern moves unknown
