Skip to content

add method keepAlive && set minimal php version to 7.4 #4 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
{
"name": "Sandesh Jangam",
"email": "[email protected]"
},
{
"name": "Jordan Hall",
"email": "[email protected]"
}
],
"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": {
Expand Down
20 changes: 19 additions & 1 deletion src/Sftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
28 changes: 21 additions & 7 deletions src/Ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/SshBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -140,6 +145,7 @@ public function timeout(int $timeout): self
return $this;
}


/**
* Returns true if the connection is established, false otherwise.
*/
Expand All @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions src/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down