首页 > 解决方案 > SSH2_Exec 执行命令的时间太长。- PHP

问题描述

我正在尝试使用 SSH2 在我的 VPS 上执行命令,但由于某种原因,第一个命令总是需要大约 10 秒才能执行。奇怪的是,在第一个命令之后,其余的命令会在 10 秒后立即执行。

我做错什么了吗?还是正常?如果是这样,我该如何解决这个问题。

class SSH 
{
public $connection = null;
public $results = "";
public $errors = "";

public function __construct($host, $port)
{
    $this->connection = ssh2_connect($host, $port);
}

public function login($username, $password)
{
    if(!$this->connection)
        return false;

    if(!ssh2_auth_password($this->connection, $username, $password))
        return false;

    return true;
}

public function execute($command)
{
    if(!$this->connection)
        return false;

    error_log("[debug] starting command.");

    $response = ssh2_exec($this->connection, $command);

    if(!$response)
        return false;

    error_log("[debug] command executed.");

    fclose($response);

    return true;
}

public function disconnect()
{
    if(!$this->connection)
        return false;

    return ssh2_disconnect($this->connection);
}
}

我已经记录了这些消息,如下所示,执行大约需要 10 秒,其余的都是即时的。

[23-Sep-2019 17:25:42 Europe/Helsinki] [debug] starting command. (command 1)
[23-Sep-2019 17:25:51 Europe/Helsinki] [debug] command executed.
[23-Sep-2019 17:25:51 Europe/Helsinki] [debug] starting command. (command 2)
[23-Sep-2019 17:25:51 Europe/Helsinki] [debug] command executed.

标签: phpssh2-execssh2

解决方案


推荐阅读