Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to call ssh2_connect() callbacks

Tags:

I am using the ssh2_connect() method to establish a connection with a remote server. A connection is establishing correctly if I am providing the right hostname and port number. When unable to connect due to wrong credentials, I am trying to call a callback-function but the way I try it isn't calling the callback after connection failure.

This i the code I tried:

$callbacks = array( 
      'ignore' => array($this, 'callbackSshDisconnect'), 
      'debug' => array($this, 'callbackSshDisconnect'), 
      'macerror' => array($this, 'callbackSshDisconnect'), 
      'disconnect' => array($this, 'callbackSshDisconnect'), 
);

ssh2_connect($hostName,$port,array('hostkey', 'ssh-rsa'),$callbacks);

public function callbackSshDisconnect($reason, $message, $language) {
    $this->log('disconnected');
    $this->log($reason);die;
}

What I am doing wrong?

like image 283
Ganesh Patil Avatar asked Aug 29 '14 07:08

Ganesh Patil


2 Answers

When ssh2_connect fails because of wrong host, port, and so on, it doesn't call any callbacks.

What is does, instead, is returning false

PHP documentation says :
Returns a resource on success, or FALSE on error.

$result = ssh2_connect($hostName,$port,array('hostkey' => 'ssh-rsa'),$callbacks);
if ($result === false)
   exit("ssh2_connect failed");

Also, reading the doc, your

array('hostkey', 'ssh-rsa')

should be

array('hostkey' => 'ssh-rsa')
like image 62
blue112 Avatar answered Oct 04 '22 07:10

blue112


There's nothing you are doing wrong apart from the typo: array('hostkey', 'ssh-rsa') should be array('hostkey' => 'ssh-rsa'). The function ssh_connect() just returns false for a connection failure; callbacks simply are not initiated yet when the wrong credentials are being used.

There are solutions (e.g. as suggested by rubo77 below) but the one I find will give you most control, and allow you to do what you want (e.g. tracking MAC errors), is to use the phpseclib library (http://phpseclib.sourceforge.net/ssh/intro.html) for ssh connections and control. It gives very fine control of commands, and also includes logging.

It's not the simplest solution, but you can control as finely as if you are at the keyboard/terminal directly.

You have control over timeouts and can use callback on commands. But if you want better control, use read() and write() and you can monitor for disconnects or other issues. Check the documentation on logging with phpseclib: you can either log and parse the log, or call getLastError().

Log files will show 'Connection closed by server' on disconnects for example, but will also tell you if you're using an unsupported authentication mode on login etc.

Or for more, read the code: here, for example, are the disconnect reasons:

    $this->disconnect_reasons = array(
        1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT',
        2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR',
        3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED',
        4 => 'NET_SSH2_DISCONNECT_RESERVED',
        5 => 'NET_SSH2_DISCONNECT_MAC_ERROR',
        6 => 'NET_SSH2_DISCONNECT_COMPRESSION_ERROR',
        7 => 'NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE',
        8 => 'NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED',
        9 => 'NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE',
        10 => 'NET_SSH2_DISCONNECT_CONNECTION_LOST',
        11 => 'NET_SSH2_DISCONNECT_BY_APPLICATION',
        12 => 'NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS',
        13 => 'NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER',
        14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE',
        15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME'
    );
like image 25
Robbie Avatar answered Oct 04 '22 06:10

Robbie