We can use the following in order to test telnet VIA port; in the following example we test port 6667:

[root@kafka03 ~]# telnet kafka02 6667
Connected to kafka02.
Escape character is '^]'.
^CConnection closed by foreign host

Since on some machines we can't use telnet (for internal reasons) what are the alternatives to check ports, as telnet?

  • 1
    Is perl an option?
    – Jeff Schaller
    Commented Nov 4, 2018 at 15:01
  • 9
    Those "internal reasons" might bar you from using other port-scanning software. I knew a guy that worked at a bank and had his contract terminated because he had a copy of nmap on his PC. He was using it for work-related purposes, but it was on the proscribed list, so he was escorted out of the building. Commented Nov 4, 2018 at 15:39
  • 3
    Is perl an option? – YES
    – yael
    Commented Nov 4, 2018 at 17:47
  • 2
    Note that telnet is a sophisticated protocol. The telnet utility turns off the protocol behaviour if a port is given at command line. Then it behaves much like netcat, just with line ending detection. Commented Nov 5, 2018 at 8:29
  • A more operating-system agnostic question, that does not even hint at port scanning, is unix.stackexchange.com/questions/499694 .
    – JdeBP
    Commented Feb 10, 2019 at 10:40

9 Answers 9


Netcat (nc) is one option.

nc -zv kafka02 6667
  • -z = sets nc to simply scan for listening daemons, without actually sending any data to them
  • -v = enables verbose mode
  • is it possible to get standard output from the nc ? because I want to write in in my bash script
    – yael
    Commented Nov 4, 2018 at 10:36
  • 4
    Read the documentation! Without options nc behaves a lot like telnet. Commented Nov 4, 2018 at 11:03
  • 1
    example - c -v -w 1 kafka01 6667 ( we not get timeout )
    – yael
    Commented Nov 4, 2018 at 11:09
  • 1
    nc -v -w 3 kafka01 6667 Ncat: Version 6.40 ( nmap.org/ncat ) Ncat: Connected to ( this still hang )
    – yael
    Commented Nov 4, 2018 at 11:10
  • 3
    ncat is not netcat. Commented Nov 4, 2018 at 15:40

If using Bash Shell, then you can use its feature to check if a port is open or closed:

(timeout 1 bash -c '</dev/tcp/ && echo PORT OPEN || echo PORT CLOSED') 2>/dev/null

(timeout 1 bash -c '</dev/tcp/ && echo PORT OPEN || echo PORT CLOSED') 2>/dev/null

Note that if the server does not respond after 1 second the timeout is reached, the commands between ' interrupted, and thus nothing is printed.

  • 6
    Perhaps you should use the hostname from the question (kafka02) instead of, which makes it look like it only works with the loopback. Commented Nov 5, 2018 at 9:52
  • 7
    (timeout 1 bash -c '</dev/tcp/www.google.com/444 && echo PORT OPEN || echo PORT CLOSED') 2>/dev/null prints nothing for me. (timeout 1 bash -c '</dev/tcp/www.google.com/444' && echo PORT OPEN || echo PORT CLOSED) 2>/dev/null worked as expected (prints PORT CLOSED). Note the location of the '.
    – thecarpy
    Commented Nov 7, 2018 at 9:21
  • what you get on bash -c '</dev/tcp/kafka01/6667'
    – yael
    Commented Nov 7, 2018 at 10:45
  • then echo $? ( if 0 then port is open ,)
    – yael
    Commented Nov 7, 2018 at 10:46
  • IIRC this bash feature used to be disabled in Debian some time ago. It's a neat trick but doesn't always work. Commented Nov 27, 2018 at 9:50

'curl' can make life easier. No root require ; curl is readily available on all the Linux systems:

  1. If port is not open will show below output

    [niti@SourceServerName ~]$ curl -vv telnet://DestinationServerName:80
    * About to connect() to DestinationServerName port 80 (#0)
    *   Trying
  2. If port is open will show below output

    [niti@SourceServerName ~]$ curl -vv telnet://DestinationServerName:443
    * About to connect() to DestinationServerName port 443 (#0)
    *   Trying
    * Connected to DestinationServerName ( port 443 (#0)

Ctrl + C to exit.

  • 1
    Why the extra v in "-vv"? I have not found any documentation that it does anything. Also, curl shows messages like "Connection refused" and "Could not resolve host" in regular (non-verbose) mode. Commented Jul 1, 2020 at 16:14
  • 1
    Good news: this also works out of the box in Windows 10 now.
    – Amit Naidu
    Commented May 9, 2022 at 23:07
  • this was the easiest and simplest for me. In my experience, for VMs behind k8s clusters it is not possible to install missing tools eg., telnet, nc etc., so curl came in quite handy. thank you!
    – mkumar118
    Commented Dec 1, 2022 at 12:20
  • Curl is absolutely not available on all linux systems, though it is available on most desktop distributions.
    – mc0e
    Commented Oct 26, 2023 at 15:38

The gold standard is undoubtedly nmap (nmap.org), but it typically requires root for “best results”. However, standalone binaries are available and it is possible to run it as an unprivileged user, just with degraded capabilities. For example, instead of a stealth syn scan (-sS), it falls back to a standard TCP connect scan (-sT). This is functionally equivalent to netcat, but with the nice multi-host, sped-up capabilities that it has.

An example:

not-root$ nmap -sT google.com
Starting Nmap 7.70 ( https://nmap.org ) at 2018-11-04 21:01 GMT
Nmap scan report for google.com (
Host is up (0.12s latency).
rDNS record for lhr35s01-in-f14.1e100.net
Not shown: 998 filtered ports
80/tcp  open  http
443/tcp open  https
  • 3
    in most organizations nmap is considered as a scaning tool and one can not use nmap without proper authorization. Also if it is an EC2 instance, authorization required from AWS as well.
    – al mamun
    Commented Nov 6, 2018 at 20:52
  • 1
    do you really think a system where telnet isn't allowed would allow nmap? Commented Apr 15, 2020 at 9:37

If Perl is an option, you can use its IO::Socket module to test a connection to a particular host and port; the script below hard-codes TCP as the protocol (which is what telnet would use):

#!/usr/bin/perl -w

# tries to connect to the given IP and port (tcp)

use strict;
use IO::Socket;

my $desthost = shift or die "Usage: $0 host port\n";
my $destport = shift or die "Usage: $0 host port\n";

gethostbyname($desthost) || die "Invalid host given\n";

my $handle = IO::Socket::INET->new(
        PeerAddr => $desthost,
        PeerPort => $destport,
        Proto    => 'tcp')
    or die "can't connect to $desthost:$destport: $!\n";
close $handle;
print "Success!\n"

Sample output from a closed port:

$ ./above-script kafka02 6667
can't connect to kafka02:6667: Connection refused

Sample output from an open port:

$ ./above-script kafka02 4200

openssl s_client -connect host:port


Device file /dev/tcp and /dev/udp can be used instead of telnet. Example: echo 0 > /dev/tcp/ . Then check the exit status using echo $? . If exit status is 0 then the port is open. If exit status is non-zero then the port is closed. For checking udp packets, use echo 0 > /dev/udp/ .

  • in my redhat 7 under /dev/ , we not have tcp
    – yael
    Commented Nov 7, 2018 at 10:47
  • ls /dev/tcp/ ls: cannot access /dev/tcp/: No such file or directory
    – yael
    Commented Nov 7, 2018 at 10:48
  • on which OS you test it?
    – yael
    Commented Nov 7, 2018 at 10:49
  • @yael, you will not get /dev/tcp or /dev/udp while ls. try the exact same command on your shell and you will get the result. by the way, I frequently use it on RHEL6,7
    – al mamun
    Commented Nov 7, 2018 at 15:29

I found fast and simple this way, throgh Python interpreter (from https://serverfault.com/a/500062/168647):

[gryphius@ut ~]$ python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2                                                                                                                                                                                                                               
Type "help", "copyright", "credits" or "license" for more information.                                                                                                                                                                                                         
>>> import socket                                                                                                                                                                                                                                                              
>>> conn=socket.create_connection(('gmail-smtp-in.l.google.com',25))                                                                                                                                                                                                           

If that didn't throw an error so far the connection is ok.

ss -lt 

this is another command you can use.

  • 1
    It works only for local machine, I believe that question is about checking the open ports of a remote host. Commented Nov 27, 2019 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .