0

I am trying to execute a command on my VPS using SSH2, but for some reason, the first command always takes around 10 seconds to execute. The wierd part is that the rest of the commands, after the first one, get executed instantly after those 10 seconds.

Did I do something wrong? Or is it normal? If so, how can I fix this.

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);
}
}

I've logged the messages and as you can see below, it takes ~10 seconds to execute and the rest are instant.

[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.
Daniel
  • 11
  • 1
  • 1

0 Answers0