How to do ssh with a timeout in a script?

后端 未结 6 1405
野趣味
野趣味 2020-12-12 11:17

I\'m executing a script connecting via password-less SSH on a remote host. I want to set a timeout, so that if the remote host is taking an infinite time to run, I want to c

6条回答
  •  余生分开走
    2020-12-12 11:26

    try this:

    timeout 5 ssh user@ip
    

    timeout executes the ssh command (with args) and sends a SIGTERM if ssh doesn't return after 5 second. for more details about timeout, read this document: http://man7.org/linux/man-pages/man1/timeout.1.html

    or you can use the param of ssh:

    ssh -o ConnectTimeout=3 user@ip
    

提交回复
热议问题