[SOLVED] SSH2 exec hanging when ethernet cable removed

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

[SOLVED] SSH2 exec hanging when ethernet cable removed

Postby daviesl5000 » Thu Oct 17, 2013 4:39 pm

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
Last edited by daviesl5000 on Fri Oct 18, 2013 3:08 pm, edited 1 time in total.
daviesl5000
Traveler
 
Posts: 3
Joined: Thu Oct 17, 2013 4:07 pm

Re: SSH2 exec hanging when ethernet cable removed

Postby TerraFrost » Thu Oct 17, 2013 5:59 pm

I'll have to play around with that and get back to you.

I'll let you know!
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: SSH2 exec hanging when ethernet cable removed

Postby daviesl5000 » Fri Oct 18, 2013 8:35 am

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
daviesl5000
Traveler
 
Posts: 3
Joined: Thu Oct 17, 2013 4:07 pm

[SOLVED] SSH2 exec hanging when ethernet cable removed

Postby daviesl5000 » Fri Oct 18, 2013 3:07 pm

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
daviesl5000
Traveler
 
Posts: 3
Joined: Thu Oct 17, 2013 4:07 pm


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 3 guests

cron