0

I wrote a php script to execute locally, on an apache server with php7.3, doing the following:

  1. access server via ssh2
  2. check if a file exists
  3. close connection
  4. printing json data object as a response.

script works without problems except for closing connection. if I add ssh2_disconnect function, no response comes back. What am I missing? Here is my code:

<?php

error_reporting(1);
ini_set('display_errors', '1');

    $config = ["server"=>"10.1.201.1","port"=>"22","user"=>"root","password"=>"root","folder"=>"/"];
    $files = ["file1.pdf","file2.pdf"];
    $result = [];
    $ftpConnect = ssh2_connect($config['server'],$config['port']);
        ssh2_auth_password($ftpConnect,$config['user'],$config['password']);
    $sftp = ssh2_sftp($ftpConnect);

foreach ($files as $file){
    $fileExists = file_exists("ssh2.sftp://". intval($sftp) . $config['folder'] . $file);
        if($fileExists){
            $result[$file]= ["status"=>"Found"];
        }else $result[$file]= ["status"=>"Not found"];
}
//ssh2_disconnect($ftpConnect); only if uncommented, script wouldn't work
header('content-type:text/json; charset=UTF-8');
echo json_encode($result);
?>
Ahmed Nasr
  • 109
  • 1
  • 10

2 Answers2

2

It seems to be version dependant. ssh2_disconnect is the only function you use to be available with PECL ssh2 >= 1.0 all the other ssh2 functions are available for PECL ssh2 >= 0.9.0. The following comment let us think php >= 7 is also needed.

http://php.net/manual/function.ssh2-disconnect.php#123413

Here is how he suggest to close the connexion without ssh2_disconnect available :

$session = null; unset($session); // close connection
Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56
Tuckbros
  • 417
  • 3
  • 13
  • if so, shouldn't I get an error back? something like error code and 'undefined function'? because no response is showing at all. – Ahmed Nasr Feb 25 '19 at 06:59
  • I tested the function existence by if(function_exists('ssh2_disconnect')) echo '1'; and it succeeded. – Ahmed Nasr Feb 25 '19 at 07:08
  • 1
    I tried to reproduce it but can't. Without ssh2_disconnect everything is fine and when I uncomment it the output is the following : {"file1.pdf":{"status":"Not found"},"file2.pdf":{"status":"Not found"}}php: channel.c:2466: _libssh2_channel_free: Assertion `session' failed. Abandon (core dumped) – Tuckbros Feb 25 '19 at 07:44
  • 1
    The function may be buggy. https://bugs.php.net/bug.php?id=73438&edit=1 – Tuckbros Feb 25 '19 at 08:39
  • thank you for your time, yes probably it is a bug and there for unset still the safer method :) – Ahmed Nasr Feb 25 '19 at 08:52
  • ssh2 is unstable: https://pecl.php.net/package/ssh2 version 1.2 - beta (2019-09-18). After change `ssh2_disconnect()` to `unset` my script was executed successfully. – TomaszKane Oct 08 '19 at 13:13
1

As it seems with more recent implementations we could successfully close the connection from SFTP with the ssh2-disconnect

So for anyone reading this, I think it's the best solution now.

Sample from the SFTP server logs:

client at xxx sent SSH_DISCONNECT message: PECL/ssh2 (http://pecl.php.net/packages/ssh2) (Application disconnected)

(Also no errors when transferring files)