diff --git a/src/Ssh.php b/src/Ssh.php index b3da00a..d4561e3 100755 --- a/src/Ssh.php +++ b/src/Ssh.php @@ -8,7 +8,7 @@ class Ssh { - protected string $user; + protected ?string $user = null; protected string $host; @@ -22,7 +22,7 @@ class Ssh private int $timeout = 0; - public function __construct(string $user, string $host, int $port = null) + public function __construct(?string $user, string $host, int $port = null) { $this->user = $user; @@ -268,11 +268,19 @@ protected function getTargetForScp(): string { $host = filter_var($this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? '[' . $this->host . ']' : $this->host; + if ($this->user === null) { + return $host; + } + return "{$this->user}@{$host}"; } protected function getTargetForSsh(): string { + if ($this->user === null) { + return $this->host; + } + return "{$this->user}@{$this->host}"; } } diff --git a/tests/SshTest.php b/tests/SshTest.php index c635909..2ab1cbe 100644 --- a/tests/SshTest.php +++ b/tests/SshTest.php @@ -157,3 +157,10 @@ assertMatchesSnapshot($command); }); + +it('can login without user', function () { + $ssh = new Ssh(null, 'example.com'); + $command = $ssh->getExecuteCommand('whoami'); + + assertMatchesSnapshot($command); +}); diff --git a/tests/__snapshots__/SshTest__it_can_login_without_user__1.txt b/tests/__snapshots__/SshTest__it_can_login_without_user__1.txt new file mode 100644 index 0000000..ba48baa --- /dev/null +++ b/tests/__snapshots__/SshTest__it_can_login_without_user__1.txt @@ -0,0 +1,3 @@ +ssh example.com 'bash -se' << \EOF-SPATIE-SSH +whoami +EOF-SPATIE-SSH \ No newline at end of file