I'm able to get around this restriction with one small issue/side-effect.
Setting the options UserKnownHostsFile=/dev/null
and StrictHostKeyChecking=no
, you can trick SSH into not actually storing or requiring verification of a host key.
Setting StrictHostKeyChecking
to no allows the connection to the server without first knowing or verifying its key; and using /dev/null
for UserKnownHostsFile
just reads and writes to nothing so no values are ever read of saved.
The caveat was that SSH still tries to create the .ssh
directory and fails, but the failure results in a warning and it will continue with the connection. The warning WILL be included in your output (unless you were to suppress warnings).
Here is an example. Note: For this example I didn't set up any authentication so it will try to use password auth and fail, but since you are using an identity you should be able to connect just fine.
<?php
$ssh_command = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "
."-p 2223 user@host.localdomain";
exec("$ssh_command ls 2>&1", $out);
var_dump($out);
// output when called from browser running as daemon(1)
array(5) {
[0]=>
string(44) "Could not create directory '/usr/sbin/.ssh'."
[1]=>
string(110) "Warning: Permanently added '[host.localdomain]:2223,[192.168.88.20]:2223' (RSA) to the list of known hosts."
[2]=>
string(36) "Permission denied, please try again."
[3]=>
string(36) "Permission denied, please try again."
[4]=>
string(82) "Received disconnect from 192.168.88.20: 2: Too many authentication failures for user"
}
Your output would most likely only include the first warning about not being able to create the .ssh
directory, followed by the warning about permanently adding the host to the list of known hosts (/dev/null
), followed by the output from your command; so you would have to check if the first line was this warning, and shift it from the $out
array.
Another note: This does open up the possibility of man-in-the-middle attacks or a DNS/IP hack to get you to try to connect to a rogue server.
See this article on SSH Host Key Protection from Symantec.