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

  • Follow


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
0
Reply andy 11/17/2003 11:57:42 PM

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

Geoff Clare probably has the answer in:

 Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
 From: xxx@xxxx.xx.xx (Geoff Clare)
 Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout)
 Message-ID: <EoBxrs.223@root.co.uk>
 Date: Fri, 13 Feb 1998 18:23:52 GMT

timeout -t <sec> <unix command>

-=-
This message was posted via two or more anonymous remailing services.




0
Reply BigappleRemailer2 (74) 11/18/2003 2:52:15 PM


In comp.security.ssh andy <andy@wpi.edu> wrote:
> 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:

[snip]

Why not just specify the connection timeout?

>         `ssh -q "$1" /bin/true &> /dev/null &`

Note that -q turns off messages, but it does not prevent the client from
asking necessary interactive questions...  You'd need BatchMode for that.

ssh -o BatchMode=yes -o ConnectTimeout=2 /bin/true > /dev/null 

-- 
Darren Dunham                                           ddunham@taos.com
Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >
0
Reply Darren 11/18/2003 11:52:17 PM

If ou have older version of ssh which does not support ConnectTimeout
you can sleep for 1 second and depend on what type of
Unix you have check /proc/<pid> or do `ps -p <pid>'.
And if process gone break out from the loop. This
will save you some time ( 1 sec) in your case.

while sleep 1; do
     [[ ! -d /proc/$pid ]] && { break 1;  }
done





"Darren Dunham" <ddunham@redwood.taos.com> wrote in message
news:Rgyub.6710$3E7.55264382@newssvr21.news.prodigy.com...
> In comp.security.ssh andy <andy@wpi.edu> wrote:
> > 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:
>
> [snip]
>
> Why not just specify the connection timeout?
>
> >         `ssh -q "$1" /bin/true &> /dev/null &`
>
> Note that -q turns off messages, but it does not prevent the client from
> asking necessary interactive questions...  You'd need BatchMode for that.
>
> ssh -o BatchMode=yes -o ConnectTimeout=2 /bin/true > /dev/null
>
> -- 
> Darren Dunham                                           ddunham@taos.com
> Unix System Administrator                    Taos - The SysAdmin Company
> Got some Dr Pepper?                           San Francisco, CA bay area
>          < This line left intentionally blank to confuse you. >


0
Reply Nikolay 11/19/2003 6:57:12 PM

I tried using
ssh -o BatchMode=yes -o ConnectTimeout=2 <ip.add.re.ss> /bin/true >
/dev/null

But it gives me the follwing error:
command-line: line 0: Bad configuration option: ConnectTimeout

I looked in my ssh_config file, and also in 'man ssh_config', and i
didnt see ConnectTimeout mentioned in either place.

Does this mean I have an older version of ssh? Is there any otehr way
I can ahceive that functionality?


Darren Dunham <ddunham@redwood.taos.com> wrote in message news:<Rgyub.6710$3E7.55264382@newssvr21.news.prodigy.com>...
> In comp.security.ssh andy <andy@wpi.edu> wrote:
> > 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:
> 
> [snip]
> 
> Why not just specify the connection timeout?
> 
> >         `ssh -q "$1" /bin/true &> /dev/null &`
> 
> Note that -q turns off messages, but it does not prevent the client from
> asking necessary interactive questions...  You'd need BatchMode for that.
> 
> ssh -o BatchMode=yes -o ConnectTimeout=2 /bin/true > /dev/null
0
Reply andy 11/19/2003 8:50:53 PM

In comp.security.ssh andy <andy@wpi.edu> wrote:
> I tried using
> ssh -o BatchMode=yes -o ConnectTimeout=2 <ip.add.re.ss> /bin/true >
> /dev/null

> But it gives me the follwing error:
> command-line: line 0: Bad configuration option: ConnectTimeout

> I looked in my ssh_config file, and also in 'man ssh_config', and i
> didnt see ConnectTimeout mentioned in either place.

> Does this mean I have an older version of ssh? Is there any otehr way
> I can ahceive that functionality?

You could fork a program that would kill the process in 2 seconds.  If
it exits, then the kill will just not work.

Something like this.  There might be some tweaking needed.

LOOP..
....
ssh -o BatchMode=yes $host /bin/true > /dev/null &
SSH_PID=$!
(sleep 2 ; kill $SSH_PID >/dev/null 2>&1)&

STATUS=wait $SSH_PID

....

The wait should block until the ssh exits, either from a normal exit or
because it's killed.

Of course you probably want to upgrade anyway.

-- 
Darren Dunham                                           ddunham@taos.com
Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >
0
Reply Darren 11/19/2003 9:43:18 PM

5 Replies
265 Views

(page loaded in 0.087 seconds)


Reply: