How to run a SSH command from www-data


How to run a SSH command from www-data



I need to run this command from php/nginx (www-data)


shell_exec("ssh -o StrictHostKeyChecking=no root@$host 'ps axo pid,cmd'")



But get this error


Failed to add the host to the list of known hosts (/root/.ssh/known_hosts).
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).





Create this directory and make it owned by www-data.
– emix
Jul 3 at 9:28


www-data




3 Answers
3



Please use "phpseclib" for a PHP SSH implementation.
Below is the example:


<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.example.com');
if (!$ssh->login('uname', 'password')) {
exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>





I dont want to type my password in the code
– clarkk
Jul 3 at 9:38





Then take it as a parameter. and this code is run on form submit.
– JavidRathod
Jul 3 at 9:39





Have a look at phpseclib.sourceforge.net/ssh/auth.html, other auth types are also supported
– Nico Haase
Jul 3 at 9:39



It's a permission issue while the www-data user doesn't have write permission to /var/www/.ssh directory (/var/www is default home folder for www-data user).



The issue should resloved by run following commands


mkdir /var/www/.ssh
chown -R www-data:www-data /var/www/.ssh



its permission error. you need to add - 'sudo' before command.


sudo shell_exec("ssh -o StrictHostKeyChecking=no root@$host 'ps axo pid,cmd'")





Since when is sudo a command of PHP?
– Nico Haase
Jul 3 at 9:39


sudo





oh yes!...sorry my bad.
– Puneet Sethi
Jul 3 at 17:32







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages