Skip to content

Instantly share code, notes, and snippets.

@Khoulaiz
Last active May 3, 2024 15:57
Show Gist options
  • Save Khoulaiz/41b387883a208d6e914b to your computer and use it in GitHub Desktop.
Save Khoulaiz/41b387883a208d6e914b to your computer and use it in GitHub Desktop.
Checking ports without telnet

Here are several different ways to test a TCP port without telnet.

$ cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_5.3
^C

$ cat < /dev/tcp/127.0.0.1/23
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/23: Connection refused

cURL

$ curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_5.3
^C

$ curl -v telnet://127.0.0.1:23
* About to connect() to 127.0.0.1 port 23 (#0)
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

Python

# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 22))
>>> clientsocket.send('\n')
1
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 23))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused

Perl

# perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
  PeerHost => '127.0.0.1',
  PeerPort => '22',
  Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
connected to the server

Credits to skohrs

@FilBot3
Copy link

FilBot3 commented Jun 20, 2016

Here is a Ruby method.

require 'socket'
require 'timeout'

module PortScanner
  class TCP
    def scan( ip, port )
      begin
        Timeout::timeout(1) do 
          begin
            s = TCPSocket.new(ip, port)
            s.close
            return true
          rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
            return false
          end
        end
      rescue Timeout::Error
      end

      return false
    end
  end
end

@amaslak
Copy link

amaslak commented Aug 22, 2016

netcat

nc -zv -w 5 127.0.0.1 22
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!

$ nc -zv -w 5 127.0.0.1 23
nc: connect to 127.0.0.1 port 23 (tcp) failed: Connection refused

Copy link

ghost commented Jul 3, 2018

loving that curl -v telnet://.... Wow.

@henrik-ch
Copy link

If you are using python 3 you need to change from->
clientsocket.send('\n')
to ->
clientsocket.send(str.encode('\n'))

@rustam18
Copy link

curl -v telnet:// .... this is awesome !!!!

@terenty-rezman
Copy link

wait for tcp port to become available:

#!/bin/sh

# wait_for_tcp.sh
# wait for tcp port to become available
# usage
# $ ./wait_for_tcp localhost:80 

host="$1"

until echo -e '\xdclose\x0d' | curl -v telnet://$host/ ; do
  >&2 echo "${host} is unavailable - sleeping..."
  sleep 1
done

echo "${host} is available"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment