doulei6778 2019-02-25 06:37
浏览 207
已采纳

当我尝试关闭ssh2连接时,没有回复

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);
?>
  • 写回答

1条回答 默认 最新

  • dougui2254 2019-02-25 06:56
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。