-1

I want to execute shell script from my php application deployed on Red Hat linux server, I added this line to my application

exec("/app/sh/script.sh user server.info.com cmd.com 0 1 1" ,
         $ret_string,
         $ret_code);

the $ret_code is 2 I tried to run /app/sh/script.sh user server.info.com cmd.com 0 1 1 on the server, it works fine there is no problem.

The content of the script /app/sh/script.sh is

ssh -x [email protected] . /app/sh/pro.sh user PRO; $EXE/cmd.com 0 1 1;

I tried to run another script that return just a string from my application and it works, so I think the problem on the ssh connection.

Why the script script.sh work when I call it from the server directly and not work when I call it from the php application ?

1
  • Does it make a difference what is in the shell code? Please make sure you reduce your scope to just include the problem. In other words, extract a minimal reproducible example. Commented Aug 23, 2022 at 7:34

1 Answer 1

0

The problem is because when you execute a command through PHP, the user is the Apache user - usually www-data - and this user can't execute ssh command.

The Apache's user can't see your private key and it's a good idea!

Instead, you have to use a SSH class like Spatie package for example :

use Spatie\Ssh\Ssh;
    
$process = Ssh::create('user', 'example.com')->execute('your favorite command');

Have a look here : https://github.com/spatie/ssh

Sign up to request clarification or add additional context in comments.

1 Comment

this user can't execute ssh command this isn’t true. Note that all that php lib does is run the same command that is in the question. As you go on to say, the apache user probably doesn’t have ssh keys that allow www-data to be identified (which also implies an alternative solution: set up keys for www-data) but that’s not the same thing as a user being unable to run a command. Relying on a convenience wrapper when it’s not strictly necessary isn’t generally a good idea - that just hides what is happening, and creates indirection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.