I am trying to control the systems ssh-agent by adding new keys to it using ssh-add. For this I'm using the Symfony Process component.
When I run this code from a web site it works perfectly fine but when I run the same code in a shell/console the ssh-add process hangs on Enter passphrase for <path to key>:
A simplified version of the code looks something like this
use Symfony\Component\Process\Process;
$keyPath = '<path to key>';
$keyPassword = '<password for unlocking the key>';
$socketPath = '<path to ssh-agent socket>';
$sshAdd = new Process(
"ssh-add {$keyPath}",
null,
[
'SSH_AUTH_SOCK' => $socketPath
],
$keyPassword
);
$sshAdd->run();
As you can see in the code above I make a call to ssh-add
, sets the SSH_AUTH_SOCK
in the environment so ssh-add
can talk to the agent and then sends the password in the input. As I said previously, when I run this in a web context it works but it hangs in a shell/console context.
I did an strace
of when running in the console and the relevant parts looks like this
open("<path to key>", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(4, "<key password>", <length of password>) = 20
close(4) = 0
wait4(9650, 0x7fff00ab3554, WNOHANG|WSTOPPED, NULL) = 0
select(8, [5 7], [], [], {0, 0}) = 0 (Timeout)
wait4(9650, 0x7fff00ab3554, WNOHANG|WSTOPPED, NULL) = 0
select(8, [5 7], [], [], {0, 0}) = 0 (Timeout)
select(8, [5 7], [], [], {0, 200000}Enter passphrase for <path to key>:) = 0 (Timeout)
select(8, [5 7], [], [], {0, 200000}) = 0 (Timeout)
select(8, [5 7], [], [], {0, 200000}) = 0 (Timeout)
select(8, [5 7], [], [], {0, 200000}) = 0 (Timeout)
select(8, [5 7], [], [], {0, 200000}) = 0 (Timeout)
...
As you can see the write seems to be ignored and the ssh-add
program starts to block waiting for input.
< /dev/null
(and possibly2</dev/null
) help it to work? – Formal