how to make SSH command execution to timeout

后端 未结 2 654
傲寒
傲寒 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:16

    It sounds like the question is asking about 'batch mode' processing against a number of hosts at once (e.g., in a loop). The following ssh command will simply fail if PKA is not configured for a given host (public key authentication -- the "password-less" login mentioned the original question), so it is good for scripts that you just want to continue running regardless of problems connecting to one or two hosts. It also won't bother with all the queries about "are you sure you want to continue connecting" to previously unknown hosts (or you can just add StrictHostKeyChecking no to your ~/.ssh/config for that). For example,

    for host in host1 host2 host3  # etc
    do
      ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
          user@$host  'uptime' || echo "problem: host=$host"
    done
    

    You'll have to check your (client) ssh man page to see if these options (BatchMode, ConnectTimeout) are supported, though.

提交回复
热议问题