Forcing SSH to timout after a certain time if it isn't responding

From: andy (andy_at_wpi.edu)
Date: 11/18/03


Date: 17 Nov 2003 15:57:42 -0800

Hi,

I'm wiritng a Bash script where I want to check if I can SSH into a
certain IP-address in a function. If the SSH call does nto respont for
2 seconds I want to kill the process, but if it responds before 2
seconds then it
shudnt have to wait unnecessarily for 2 seconds.

here the code i was trying:

# tests if node can be SSH'd into
checkSSH()
{
        `ssh -q "$1" /bin/true &> /dev/null &`
        pid=$!
      
        # waiting
        (
                sleep 2
                if kill -0 $pid > /dev/null 2>&1
                then
                        kill -TERM $pid
                fi
        ) &
       wait $pid

        if (( $? == 0 ))
        then
                echo "$1"
        fi
}

This function is provided with IP addresses where it can SSH into.
I think this works fine, but the function always waits for 2 seconds
which I
do not want to happen. Is there a way of implementing it?

Thanks,

-andy



Relevant Pages