0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check if Port is Open

Use nc -zv hostname port in a Bash script to check if a port is open; for example, nc -zv 127.0.0.1 80 tests port 80 on localhost.
📋

Examples

Input127.0.0.1 22
OutputConnection to 127.0.0.1 22 port [tcp/ssh] succeeded!
Inputgoogle.com 80
OutputConnection to google.com 80 port [tcp/http] succeeded!
Input127.0.0.1 9999
Outputnc: connect to 127.0.0.1 port 9999 (tcp) failed: Connection refused
🧠

How to Think About It

To check if a port is open, the script tries to connect to the given hostname and port. If the connection succeeds, the port is open; if it fails, the port is closed or blocked. The script uses a simple network tool to attempt this connection and reads the result.
📐

Algorithm

1
Get the hostname or IP address and port number as input.
2
Try to open a TCP connection to the given host and port.
3
Check if the connection was successful or failed.
4
Print a message indicating if the port is open or closed.
💻

Code

bash
#!/bin/bash
host=$1
port=$2

if nc -zv "$host" "$port" 2>&1 | grep -q 'succeeded'; then
  echo "Port $port on $host is open."
else
  echo "Port $port on $host is closed or unreachable."
fi
Output
Port 22 on 127.0.0.1 is open.
🔍

Dry Run

Let's trace checking port 22 on 127.0.0.1 through the code

1

Set variables

host=127.0.0.1, port=22

2

Run nc command

nc -zv 127.0.0.1 22

3

Check output

Output contains 'succeeded', so port is open

StepCommandOutput snippet
1Set host and porthost=127.0.0.1, port=22
2nc -zv 127.0.0.1 22Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
3grep 'succeeded'Found, port is open
💡

Why This Works

Step 1: Use nc to test connection

The nc -zv command tries to connect to the specified host and port without sending data.

Step 2: Check command output

If the output contains succeeded, it means the port is open and accepting connections.

Step 3: Print result

The script prints a clear message based on whether the port is open or closed.

🔄

Alternative Approaches

Using /dev/tcp special file
bash
#!/bin/bash
host=$1
port=$2

if timeout 1 bash -c "</dev/tcp/$host/$port" 2>/dev/null; then
  echo "Port $port on $host is open."
else
  echo "Port $port on $host is closed or unreachable."
fi
This method uses Bash's built-in TCP connection feature but may not work on all systems and requires Bash.
Using telnet command
bash
#!/bin/bash
host=$1
port=$2

if echo > /dev/tcp/$host/$port 2>/dev/null; then
  echo "Port $port on $host is open."
else
  echo "Port $port on $host is closed or unreachable."
fi
This uses Bash's /dev/tcp but is simpler; however, it may hang if the port is filtered without response.

Complexity: O(1) time, O(1) space

Time Complexity

The script runs a single network connection attempt, so it takes constant time regardless of input size.

Space Complexity

The script uses a fixed amount of memory for variables and command execution, so space is constant.

Which Approach is Fastest?

Using nc is generally fast and reliable; the /dev/tcp method is lightweight but less portable.

ApproachTimeSpaceBest For
nc -zvO(1)O(1)Reliable port check with clear output
/dev/tcpO(1)O(1)Lightweight, Bash-only environments
telnetO(1)O(1)Legacy systems without nc
💡
Use nc -zv with a timeout to avoid long waits on unreachable ports.
⚠️
Forgetting to redirect error output or not checking the command's success properly can cause wrong results.