1

We have an old system in which from a set of computers, exactly one is configured as the "live" machine and the rest are fail-overs. We used to use ping to determine which was which, however, security updates now render ping useless.

I have found that ssh is still available and I can do essentially the same thing using ssh, however, I don't see that ssh has the same timeout option as does ping (we used a 1 second timeout which was more than adequate in our environment). I changed the script to use use ssh (running uname) and the process will work, except that I don't have the timeout option, when the script hits the machine that has been reconfigured, ssh hangs for a very long time.

Is there anyway to set a short time out for ssh to connect or find some other way to kill it after 1-2 seconds? Of course, if there is another way to see quickly if any machine responds to a hostname, or rather if no machine responds to a hostname, I'd love to know that, too.

Thanks

  • ping is never a goot tool for that... you should "monitor" things using a check that mimicks the real traffic of the application you are monitoring. For example, if it is a webserver; you do an HTTP query, etc. – Patrick Mevzek Feb 1 '18 at 2:02
  • Patrick - Well, we could debate about ping as it had been working, but ultimately, since ping was disabled, its no longer a candidate. I thought about your exact solution, however, the problem is that the server will always respond looking like the "xyz" server - when they reconfigure, the live machine has the IP address and hostname of the official system, while the other machines take on their "alternate egos". A query as you suggest would always reply "Server XYZ is at IP 1.2.3.4". I could have reported a MAC address, I suppose. – Jim Feb 2 '18 at 13:21
2

Yes, you can use the ConnectTimeout option. From man ssh_config:

ConnectTimeout: Specifies the timeout (in seconds) used when connecting to the SSH server, instead of using the default system TCP timeout. This value is used only when the target is down or really unreachable, not when it refuses the connection.

Therefore, for a 5 second timeout:

ssh -o ConnectTimeout=5 host true

Note that this won't handle the case that you connect quickly, but the actual command you're running is slow, either on the connecting machine or the machine which you're connecting to. You may want to handle that case by both using ConnectTimeout and the timeout binary from GNU coreutils with a higher timeout as a backup.

For example, this will time out connections that take over 5 seconds, and will also terminate if anything in the entire operation takes over 7 seconds (by sending SIGTERM). 3 seconds after that, if ssh still didn't quit, we send SIGKILL:

timeout 7 -k 3 ssh -o ConnectTimeout=5 host true

Whether you also want to use timeout or not depends on what you want to achieve, but it will at least allow you to fail fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.