curl / Mailing Lists / curl-users / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Use curl to check the status/availability of local/remote port(s).

From: Dan Fandrich via curl-users <>
Date: Mon, 11 Jan 2021 16:00:06 -0800

On Mon, Jan 11, 2021 at 10:25:01PM +0100, Daniel Stenberg via curl-users wrote:
> On Mon, 11 Jan 2021, Hongyi Zhao via curl-users wrote:
> > Say, for checking the availability of telnet server, I can use nc as
> > follows:
> >
> > $ nc -z hostIP 23
> >
> > But I want to know if I can do the same job with curl too.
> You *can* but it won't be as convenient:
> Is there a SSH port listening?
> $ curl --http0.9 --connect-timeout 3 localhost:22 -s -o /dev/null

Keep in mind that this isn't an exact replication of the equivalent nc command.
The big difference is that if the port is open, curl will send bytes that
correspond to a URL request (HTTP in the example above). If a server (like an
SSH server) is listening on the port, it should ignore these bytes (if it's not
an HTTP server) and probably log an error, or it will start a transfer (if it's
an HTTP server). This may be harmless or it may not be, depending on your

Also, if the port is open and the server sends nothing (e.g. if it's an HTTP
server) the above command will hang. Add --max-time 3 to fix that and look for
the 28 error code that indicates this situation.

You can reduce the number of bytes curl sends in this case, but I can't think
of a way to make it send nothing if the port is open. Something like will send
only two bytes:

$ curl --connect-timeout 3 --max-time 3 -s -o /dev/null gopher://localhost:22

This one will send nothing unless the server is an FTP server (it will also be
fooled by SMTP and probably other protocols):

$ curl --connect-timeout 3 --max-time 3 -s -o /dev/null ftp://localhost:22

Received on 2021-01-12