我正在尝试使用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.