2

Helli,

I have a script that waits some time until a machine (being provisioned) comes up and then it connects to ssh and executes something. I use "sleep" for that.

I would like change my script to actively check the port in a bash loop and when it's ready (sshd is started and accepting connections) it would continue.

How to do that? Is there an UNIX command that is able to tell me if the TCP server port is responding?

2 Answers 2

2

Why don't you simply call ssh? It will wait until the remote host is available and time out after a while. If an error occurs, it will return 255, which means that you can retry it.

2
  • until ssh -o ConnectTimeout=4 hostname; do sleep 1; echo Retrying...; done << would work but I am looking for something else (actually in my case its not ssh)
    – lzap
    May 11, 2012 at 9:53
  • Ok so then use for example netcat: nc -z -w 4 <ip> <port>. Returns 0 if the connection was successful, 1 if not.
    – Oliver
    May 11, 2012 at 9:56
1

As suggested in https://stackoverflow.com/a/50055449/15820811, you can use:

until printf "" 2>>/dev/null >>/dev/tcp/mysshserver.example.org/22; do sleep 1; done

This tries to write data to port 22 (the SSH port) on mysshserver.example.org (replace with your server) every second until the data could be written successfully.

You must log in to answer this question.

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