How to check if port is opened without telnet OR custom script ?

Sometimes we might need to check if a specific network port is opened for various reasons. There are various tool available out there and most of them serves the purpose. In this article, we will talk about few tools that can be used to check if a port is opened, without needing to write a script or code.

  1. Telnet
  2. Bash
  3. cURL
  4. nmap
  5. netcat (nc)

Telnet

We most of the time turn towards using telnet command for this. And telnet works great, all you need to do is use telnet host:port and you can check if port is opened or not.

telnet {host} {port}

Example: Check if port is opened using telnet.

Bash
kali@\:~ $ telnet 127.0.0.1 22
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
^C
Connection closed by foreign host.

kali@\:~ $ telnet 127.0.0.1 23
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
kali@\:~ $

Bash

telnet works very well, until you don’t have telnet in your machine. Now you can if you want write a shell/python OR pert script to achieve the same, OR if you have bash you can use this to check port status.

cat < /dev/tcp/{host}/{port}

Example: Check if port is opened using Bash.

Bash
kali@\:~ $ cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
^C
kali@\:~ $
kali@\:~ $
kali@\:~ $ cat < /dev/tcp/127.0.0.1/23
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/23: Connection refused
kali@\:~ $
kali@\:~ $

cURL

Above is also a good and quick way to check opened ports. But what if you are working on windows, and don’t have access to BASH shell ? Well, in that case we can use curl to check if a remote port is opened or not.

curl -v telnet://{host}:{port}

Example: Check if port is opened using curl.

Bash
C:\Users>curl -v telnet://127.0.0.1:22
*   Trying 127.0.0.1:22...
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
^C
C:\Users>
C:\Users>curl -v telnet://127.0.0.1:23
*   Trying 127.0.0.1:23...
* connect to 127.0.0.1 port 23 failed: Connection refused
* Failed to connect to 127.0.0.1 port 23 after 2027 ms: Couldn't connect to server
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 23 after 2027 ms: Couldn't connect to server

C:\Users>

Above is also helpful when you are working with containers images, that doesn’t have telnet binaries installed, unless you prefer to write a small script of-course. But sometimes telnet protocol is not enabled in curl, and you may get an error like ” Protocol "telnet" not supported or disabled in libcurl “.

Bash
sh-5.1$ curl -v "telnet://localhost:9080"
* Protocol "telnet" not supported or disabled in libcurl
* Closing connection -1
curl: (1) Protocol "telnet" not supported or disabled in libcurl
sh-5.1$

Nmap: the Network Mapper

Last but not the least is nmap, which a very powerful utility, used to discover hosts and services on a computer network by sending packets and analyzing the responses. Now nmap is world’s leading port scanner, and has lots of use cases, which are out of scope for the purpose of this article, and we can use this to check if a single port is opened.

nmap -p {port} {host}

Example: Using nmap to check single port.

Bash
kali@\:~ $ nmap -p 22 127.0.0.1
Warning: Nmap may not work correctly on Windows Subsystem for Linux.
For best performance and accuracy, use the native Windows build from https://nmap.org/download.html#windows.
Starting Nmap 7.80 ( https://nmap.org ) at 2023-07-04 05:35 EDT
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0017s latency).

PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
kali@\:~ $
kali@\:~ $ nmap -p 23 127.0.0.1
Warning: Nmap may not work correctly on Windows Subsystem for Linux.
For best performance and accuracy, use the native Windows build from https://nmap.org/download.html#windows.
Starting Nmap 7.80 ( https://nmap.org ) at 2023-07-04 05:35 EDT
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0018s latency).

PORT   STATE  SERVICE
23/tcp closed telnet

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
kali@\:~ $

netcat (nc)

Similar to nmap, netcat OR nc is a networking utility for reading from and writing to network connections using TCP or UDP. We can also use netcat(nc) to check if a port is opened.

nc -v {host} {port}

Example: Using netcat to check single port.

Bash
kali@\:~ $ nc -v 127.0.0.1 22
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
^C
kali@\:~ $
kali@\:~ $ nc -v 127.0.0.1 23
nc: connect to 127.0.0.1 port 23 (tcp) failed: Connection refused
kali@\:~ $

Hope this helps.

Note: Unauthorized port scanning, for any reason, is strictly prohibited.

thank you.

Leave a comment