Page 1 of 1

[SOLVED] SSH2 exec hanging when ethernet cable removed

PostPosted: Thu Oct 17, 2013 4:39 pm
by daviesl5000
Hi Everyone,

Apologies if this issues has already been covered (I did try searching, honest), but I've hit a brick wall!

I'm using phpseclib on Linux. I've written a small snippet to detect whether a SSH2 link is still active by checking if the /bin/bash file is present.

Code: Select all
<?php
set_include_path ( get_include_path() . PATH_SEPARATOR . '/root/php/MPlayerMonitor/phpseclib' ) ;
include_once "/root/php/MPlayerMonitor/phpseclib/Net/SSH2.php" ;

$SSH = new Net_SSH2("10.8.101.141");

// Connect to remote device
if (!($SSH->login("root","password")))
{
    printf("Failed to connect to remote device\n");
    exit(1);
}

// Set timeout
$SSH->setTimeout(5);

for ( ; ; )
{
    // Check /bin/bash file is present (it should be)
    $Result=$SSH->exec("ls /bin/bash" ) ;
    $Result=trim($Result,"\n\r");
   
    if ( $Result == "/bin/bash" )
        printf ( "Connected\n" ) ;
    else
        printf ( "Disconnected\n" ) ;

    sleep ( 2 ) ;
}
?>


This routine will connect to the remote device (10.8.102.41) and quite happily spit out "Connected" messages. If I reboot the remote device I get the following ...

Code: Select all
Connected
Connected
Connected
Connected
PHP Notice:  Connection closed prematurely in /display/scripts/MPlayerMonitor/phpseclib/Net/SSH2.php on line 2347
Disconnected
PHP Notice:  Connection closed prematurely in /display/scripts/MPlayerMonitor/phpseclib/Net/SSH2.php on line 2347
Disconnected


Great. However if I try the same steps again but remove the ethernet cable from the remote device rather than rebooting it (a dirty disconnect), the exec never returns...

Code: Select all
Connected
Connected
Connected
Connected


Any ideas?

Cheers,

Lee

Re: SSH2 exec hanging when ethernet cable removed

PostPosted: Thu Oct 17, 2013 5:59 pm
by TerraFrost
I'll have to play around with that and get back to you.

I'll let you know!

Re: SSH2 exec hanging when ethernet cable removed

PostPosted: Fri Oct 18, 2013 8:35 am
by daviesl5000
Thanks for the response TerraFrost :)

It seems that the command line version of SSH behaves in the same way. If you try the following -

1. SSH onto a remote device from the command line
2. Run top
3. Reboot remote device

The top session terminates with the following response

Code: Select all
Connection to 10.8.101.141 closed by remote host.
Connection to 10.8.101.141 closed


If you then try the following -

1. SSH onto a remote device from the command line
2. Run top
3. Pull the ethernet cable out

The top session is frozen indefinitely and the command line is inoperable. Another strange effect is if you plug the ethernet cable back in, the top program starts working again!

It's as though the SSH receive socket sits there indefinitely waiting for data that's never going to arrive?

Hope this helps,

Lee

[SOLVED] SSH2 exec hanging when ethernet cable removed

PostPosted: Fri Oct 18, 2013 3:07 pm
by daviesl5000
I was using an older version of phpseclib. I've just tried the latest version (0.3.5) and exec no longer hangs, even when the Ethernet cable is pulled :)

Thanks for your help.

Lee