Skip to content

Commit 8a69221

Browse files
authored
Merge pull request #98 from pyrech/connect-without-user
Allow SSH connection without user
2 parents dabaf6e + 3df910a commit 8a69221

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/Ssh.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Ssh
1010
{
11-
protected string $user;
11+
protected ?string $user = null;
1212

1313
protected string $host;
1414

@@ -22,7 +22,7 @@ class Ssh
2222

2323
private int $timeout = 0;
2424

25-
public function __construct(string $user, string $host, int $port = null)
25+
public function __construct(?string $user, string $host, int $port = null)
2626
{
2727
$this->user = $user;
2828

@@ -268,11 +268,19 @@ protected function getTargetForScp(): string
268268
{
269269
$host = filter_var($this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? '[' . $this->host . ']' : $this->host;
270270

271+
if ($this->user === null) {
272+
return $host;
273+
}
274+
271275
return "{$this->user}@{$host}";
272276
}
273277

274278
protected function getTargetForSsh(): string
275279
{
280+
if ($this->user === null) {
281+
return $this->host;
282+
}
283+
276284
return "{$this->user}@{$this->host}";
277285
}
278286
}

tests/SshTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,10 @@
157157

158158
assertMatchesSnapshot($command);
159159
});
160+
161+
it('can login without user', function () {
162+
$ssh = new Ssh(null, 'example.com');
163+
$command = $ssh->getExecuteCommand('whoami');
164+
165+
assertMatchesSnapshot($command);
166+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ssh example.com 'bash -se' << \EOF-SPATIE-SSH
2+
whoami
3+
EOF-SPATIE-SSH

0 commit comments

Comments
 (0)