diff --git a/README.md b/README.md index 34da021..82d10db 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ Install the package via composer: composer require sandeshjangam/tiny-ssh-php ``` +## Requirements + +- Php >= 7.4 +- phpseclib ^3.0 + ## Usage Simple SSH command using password authentication: diff --git a/composer.json b/composer.json index db88597..2099aba 100644 --- a/composer.json +++ b/composer.json @@ -13,15 +13,19 @@ { "name": "Sandesh Jangam", "email": "sandesh2643@gmail.com" + }, + { + "name": "Jordan Hall", + "email": "jordan@hall05.co.uk" } ], "require": { - "php": ">=7.1", + "php": ">=7.4", "phpseclib/phpseclib": "^3.0" }, "require-dev": { - "nunomaduro/phpinsights": "^2.11", - "pestphp/pest": "^2.35" + "nunomaduro/phpinsights": "*", + "pestphp/pest": "*" }, "config": { "allow-plugins": { diff --git a/src/Sftp.php b/src/Sftp.php index 5cb1741..e3cd387 100644 --- a/src/Sftp.php +++ b/src/Sftp.php @@ -14,7 +14,7 @@ final class Sftp extends SshBase /** * The SSH2 connection object. */ - private SFTP2 $sftp; + private ?SFTP2 $sftp; /** * Connects to the remote server using the configured details. @@ -35,6 +35,10 @@ public function connect(): self $this->sftp->setTimeout($this->timeout); } + if ($this->keepAlive) { + $this->sftp->setKeepAlive($this->keepAlive); + } + $this->connected = true; return $this; @@ -99,6 +103,20 @@ public function getFingerprint(): string return $this->sftp->getServerPublicHostKey(); } + /** + * Set Keep Alive. Default to no keep alive. + * + * @param int $interval + */ + public function keepAlive($interval): self + { + $this->keepAlive = $interval; + if ($this->sftp) { + $this->sftp->setKeepAlive($this->keepAlive); + } + return $this ; + } + /** * Authenticates with the SFTP server using either a private key or a password. * diff --git a/src/Ssh.php b/src/Ssh.php index 7c2629d..054b1db 100644 --- a/src/Ssh.php +++ b/src/Ssh.php @@ -13,7 +13,7 @@ final class Ssh extends SshBase /** * The SSH2 connection object. */ - private SSH2 $ssh; + private ?SSH2 $ssh = null; /** * Connects to the remote server using the configured details. @@ -34,6 +34,10 @@ public function connect(): self $this->ssh->setTimeout($this->timeout); } + if ($this->keepAlive) { + $this->ssh->setKeepAlive($this->keepAlive); + } + $this->connected = true; return $this; @@ -57,14 +61,10 @@ public function disconnect(): void /** * Execute a command * + * @param string|array $command $command * @throws RuntimeException */ - /** - * Execute a command - * - * @throws RuntimeException - */ - public function execute(string|array $command): SshCommand + public function execute($command): SshCommand { if (! $this->connected) { throw new RuntimeException('Could not execute command. ' . self::ERROR_NOT_CONNECTED); @@ -89,6 +89,20 @@ public function getFingerprint(): string return $this->ssh->getServerPublicHostKey(); } + /** + * Set Keep Alive. Default to no keep alive. + * + * @param int $interval + */ + public function keepAlive($interval): self + { + $this->keepAlive = $interval; + if ($this->ssh) { + $this->ssh->setKeepAlive($this->keepAlive); + } + return $this ; + } + /** * Authenticates with the SSH server using either a private key or a password. * diff --git a/src/SshBase.php b/src/SshBase.php index e1bb893..1ddfb3a 100644 --- a/src/SshBase.php +++ b/src/SshBase.php @@ -46,6 +46,11 @@ abstract class SshBase */ protected int $timeout = 10; + /** + * The Keep Alive Interval in seconds for the SSH connection. Defaults to 10. + */ + protected int $keepAlive = 10; + /** * Whether the SSH connection has been established. */ @@ -140,6 +145,7 @@ public function timeout(int $timeout): self return $this; } + /** * Returns true if the connection is established, false otherwise. */ @@ -158,6 +164,8 @@ abstract public function connect(): self; */ abstract public function disconnect(): void; + abstract public function keepAlive($interval): self; + /** * Validates the connection arguments. * If any of the required arguments are empty, an exception will be thrown. diff --git a/src/SshCommand.php b/src/SshCommand.php index bc0f6ff..b61fa32 100644 --- a/src/SshCommand.php +++ b/src/SshCommand.php @@ -20,8 +20,9 @@ final class SshCommand /** * The output of the command. + * @var string|false */ - private string|false $output; + private $output; /** * The error output of the command. @@ -30,8 +31,9 @@ final class SshCommand /** * The exit status of the command. + * @var string|false */ - private int|false $exitStatus; + private $exitStatus; /** * Constructs a new instance of the SshCommand class. @@ -69,7 +71,7 @@ public function execute(): self * * @return bool|string The raw output, or false if no output was captured. */ - public function getRawOutput(): string|false + public function getRawOutput() { return $this->output; } @@ -102,16 +104,18 @@ public function getError(): string /** * Returns the exit status of the command. + * @return int|false */ - public function getExitStatus(): int|false + public function getExitStatus() { return $this->exitStatus; } /** * Check if the exit status of the command matches the given status. + * @param int|false $status */ - public function isExitStatus(int|false $status): bool + public function isExitStatus($status): bool { return $this->exitStatus === $status; }