How to do ssh with a timeout in a script?

后端 未结 6 1352
野趣味
野趣味 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
    
    0 讨论(0)
  • 2020-12-12 11:34
    ssh -o ConnectTimeout=10  <hostName>
    

    Where 10 is time in seconds. This Timeout applies only to the creation of the connection.

    0 讨论(0)
  • 2020-12-12 11:35

    Well, you could use nohup to run whatever you are running on 'non-blocking mode'. So you can just keep checking if whatever it was supposed to run, ran, otherwise exit.

    nohup ./my-script-that-may-take-long-to-finish.sh &
    ./check-if-previous-script-ran-or-exit.sh
    
    
    echo "Script ended on Feb 15, 2011, 9:20AM" > /tmp/done.txt
    

    So in the second one you just check if the file exists.

    0 讨论(0)
  • 2020-12-12 11:39

    You could also connect with flag

    -o ServerAliveInterval=<secs>
    so the SSH client will send a null packet to the server each <secs> seconds, just to keep the connection alive. In Linux this could be also set globally in /etc/ssh/ssh_config or per-user in ~/.ssh/config.

    0 讨论(0)
  • 2020-12-12 11:41

    If all else fails (including not having the timeout command) the concept in this shell script will work:

     #!/bin/bash
     set -u
     ssh $1 "sleep 10 ; uptime" > /tmp/outputfile 2>&1 & PIDssh=$!
     Count=0
     while test $Count -lt 5 && ps -p $PIDssh > /dev/null
     do
        echo -n .
        sleep 1
        Count=$((Count+1))
     done
     echo ""
    
     if ps -p $PIDssh > /dev/null
     then
        echo "ssh still running, killing it"
        kill -HUP $PIDssh
     else
        echo "Exited"
     fi
    
    0 讨论(0)
  • 2020-12-12 11:42

    Use the -o ConnectTimeout and -o BatchMode=yes -o StrictHostKeyChecking=no .

    ConnectTimeout keeps the script from hanging, BatchMode keeps it from hanging with Host unknown, YES to add to known_hosts, and StrictHostKeyChecking adds the fingerprint automatically.

    **** NOTE **** The "StrictHostKeyChecking" was only intended for internal networks where you trust you hosts. Depending on the version of the SSH client, the "Are you sure you want to add your fingerprint" can cause the client to hang indefinitely (mainly old versions running on AIX). Most modern versions do not suffer from this issue. If you have to deal with fingerprints with multiple hosts, I recommend maintaining the known_hosts file with some sort of configuration management tool like puppet/ansible/chef/salt/etc.

    0 讨论(0)
提交回复
热议问题