Skip to main content

Check open port without telnet

· 2 min read
VoidQuark
Open-source enthusiast

Check open port without telnet

This post cover alternatives for checking open port without telnet. Some companies have strict policies and it can happen that telnet is not available.

And you know what? There is no requirement to install telnet. Just check the below alternatives.

Bash method

Bash is still the most popular shell. It is good to know this method because it is quite practical. It opens UDP or TCP connection to the corresponding socket.

  • Successful connection
$ cat < /dev/tcp/localhost/22
SSH-2.0-OpenSSH_8.0
  • Failed connection
$ cat < /dev/tcp/localhost/23
bash: connect: Connection refused
bash: /dev/tcp/localhost/23: Connection refused

Netcat method

Netcat is able to connect and listen on any port. It supports TCP/UDP ports.

  • Successful connection
nc -zv -w 5 localhost 22
Ncat: Connected to localhost:22
  • Failed connection
nc -zv -w 5 localhost 23
Ncat: Connection refused.

Curl method

Curl command is used to transfer data. It is great that it supports telnet protocol.

  • Successful connection
curl telnet://localhost:22
SSH-2.0-OpenSSH_8.0
  • Failed connection
curl telnet://localhost:23
curl: (7) Failed to connect to localhost port 23 after 0 ms: Connection refused

Python3 method

Python socket library provides direct access to the operating system socket interface.

  • Successful connection
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('localhost', 22))
>>> clientsocket.send(str.encode('\n'))
1
  • Failed connection
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('localhost', 23))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ConnectionRefusedError: [Errno 111] Connection refused

Perl method

Perl module named IO::Socket allows us to open the socket interface.

  • Successful connection
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
PeerHost => 'localhost',
PeerPort => '22',
Proto => 'tcp',
);
die "$!\n" unless $socket;
print "Connected\n";
## PRESS CTRL+D !
Connected
  • Failed connection
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
PeerHost => 'localhost',
PeerPort => '23',
Proto => 'tcp',
);
die "$!\n" unless $socket;
print "Connected\n";
## PRESS CTRL+D !
Connection refused

Thanks for reading. I'm entering the void. 🛸 ➡️ 🕳️