If you need to script such a test, the solution by Serhii Popov (see comment to question) is probably the best since nc
is capable of searching the TCP stack for an open port³ instead of attempting an actual connection.
The simplest form is:
nc -z <ip> <port>
The command returns true if it find the specified <ip>:<port>
combo as being opened (i.e. one of your services is listening).
So now you can write a script to wait until the port is open:
while ! nc -z <ip> <port>
do
sleep 1
done
Note 1: I tried the -w
command line option and that did not seem to do anything. Either way the command returns immediately. I think that the -w
is not useful with -z
.
Note 2: to help debug, try with the -v
command line option.
_Note 3: nc -z ...
actually creates a socket()
and then attempts to bind()
it and connect()
. If that works, it deems the port open.Note 3: nc -z ...
actually creates a socket()
and then attempts to bind()
it and connect()
. If that works, it deems the port open.