Skip to content

Commit 8d274a9

Browse files
authored
Fix hostname returned when multiple host aliases are defined (#250)
For example, given config: Host foo bar baz HostName 127.0.0.1 The hostname returned will return 'foo' instead of 'foo bar baz' (invalid). Fixes #230
1 parent aa9fe0f commit 8d274a9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/SSHConfigFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function findConfiguredHost($host)
120120
}
121121
}
122122

123-
return $group['host'];
123+
return preg_replace('/\s+.*$/', '', $group['host']);
124124
}
125125
}
126126
}

tests/SSHConfigFileTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ public function test_it_returns_null_for_a_matching_host_if_user_specified_and_i
247247
$this->assertNull($host);
248248
}
249249

250+
public function test_it_returns_valid_host_for_multiple_aliased_hosts()
251+
{
252+
$config = <<<'EOT'
253+
Host foo bar baz
254+
Hostname baz.com
255+
User john
256+
EOT;
257+
$sshConfig = SSHConfigFile::parseString($config);
258+
$host = $sshConfig->findConfiguredHost('baz.com');
259+
260+
// Really, any one of the host's aliases would work
261+
$this->assertContains($host, ['foo', 'bar', 'baz']);
262+
}
263+
250264
private function parse($config)
251265
{
252266
$sshConfig = SSHConfigFile::parseString($config);

0 commit comments

Comments
 (0)