Skip to content

Instantly share code, notes, and snippets.

@ildar-shaimordanov
Forked from Khoulaiz/gist:41b387883a208d6e914b
Last active March 13, 2024 12:16
Show Gist options
  • Save ildar-shaimordanov/9281487d010782b1eebcc35c86299fe6 to your computer and use it in GitHub Desktop.
Save ildar-shaimordanov/9281487d010782b1eebcc35c86299fe6 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 -c 'import socket; socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("127.0.0.1", 22))'

perl

perl -MIO::Socket::INET \
-e 'new IO::Socket::INET(PeerHost => $host, PeerPort => $port, Proto => $proto) or die "Cannot connect: $!\n"' \
-s -- \
-proto=tcp \
-host=localhost \
-port=22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment