I'm working on an EC2 box and trying to SSH command another box.
The command works in command-line, even php -a interactive. However it does not work when running as apache. Example cmd:-
system('ssh -i /home/me/keys/key.pem [email protected] "ls"');
I've tried adding apache to wheel group, and gshadow on both boxes. I've also just tried chowning the pem file to apache. Nothing.
Yet the command response fine in the two other use cases outlines.
What's going on here? Anyone know?
You need to specify the user in the ssh command options:
This probably still won't work unless apache has access to your key.pem file which would be a huge security vulnerability. I really suggest using some ssh library (like others have suggested) and setting up apache with it's own ssh keys, and set up the server it's connecting to with reduced permission access to that account.
First; try to run it as the webserver user, for example:
Second; make sure you not only capture the result of the command you are executing, but the lines it throws back. You can do this in PHP with:
http://www.php.net/manual/en/function.exec.php
Third: please do come back with a working answer.
you need to use php openssh library to properly run these commands, otherwise, it can compromise security of your system.
My recommendation would be to use phpseclib, a pure PHP SSH implementation. eg.
Use pecl extension http://php.net/manual/en/book.ssh2.php