how to make SSH command execution to timeout

后端 未结 2 653
傲寒
傲寒 2021-02-15 00:00

I have a program like this:

ssh -q harigm@8.19.71.238 exit
echo \"output value  -->$?\"

In the above code, I am trying to SSH to the remote

2条回答
  •  我在风中等你
    2021-02-15 00:06

    You could wrap the call to ssh using the timeout command. The timeout command exits with code 124 if a timeout occurs.

    timeout 10s ssh -q harigm@8.19.71.238 exit
    if [ $? -eq 124 ]; then
        echo "Timeout out"
    fi
    

    Or, as Vorsprung has commented on your question (as I was looking up the man page!):

    ssh -oPasswordAuthentication=no -q harigm@8.19.71.238 exit
    

    which will disallow interactive password authentication. You'd then have to check the exit code.

提交回复
热议问题