Skip to content

Commit

Permalink
Merge pull request #98 from pyrech/connect-without-user
Browse files Browse the repository at this point in the history
Allow SSH connection without user
  • Loading branch information
freekmurze authored Apr 5, 2024
2 parents dabaf6e + 3df910a commit 8a69221
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Ssh
{
protected string $user;
protected ?string $user = null;

protected string $host;

Expand All @@ -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;

Expand Down Expand Up @@ -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}";
}
}
7 changes: 7 additions & 0 deletions tests/SshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,10 @@

assertMatchesSnapshot($command);
});

it('can login without user', function () {
$ssh = new Ssh(null, 'example.com');
$command = $ssh->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});
3 changes: 3 additions & 0 deletions tests/__snapshots__/SshTest__it_can_login_without_user__1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ssh example.com 'bash -se' << \EOF-SPATIE-SSH
whoami
EOF-SPATIE-SSH

0 comments on commit 8a69221

Please sign in to comment.