2

Morning guys,

Am having trouble escaping a string with variables that I want to pass to exec(). Here is the string:

$send_telemetry = exec ( 'echo "Temp 1="'.$temp_1[0].'" Pressure 1="'.$pressure_1[0].'" Flow 1 ="'.$flow_1[0].'" | mutt -s "Telemetry values for AREA 11C" [email protected]' ) ;
echo "$send_telemetry" ;

I had this working before, but some over eager newbie technician thought it a smart idea to change the code and he ended up making a mess out of it. I am aware of shell_exec() but I prefer exec().

Thank you

1 Answer 1

4

from the PHP website about the function escapeshellarg

escapeshellarg() adds single quotes around a string and quotes/escapes any existing single quotes allowing you to pass a string directly to a shell function and having it be treated as a single safe argument. This function should be used to escape individual arguments to shell functions coming from user input. The shell functions include exec(), system() and the backtick operator.

So what you should probably be doing is escaping any variables that you are passing into your command, like so...

$send_telemetry = exec ( 'echo "Temp 1=" ' . escapeshellarg($temp_1[0]) . ' "Pressure 1=" ' . escapeshellarg($pressure_1[0]) . ' "Flow 1 =" ' . escapeshellarg($flow_1[0]) . ' " | mutt -s "Telemetry values for AREA 11C" [email protected]' ) ;
echo "$send_telemetry" ;
Sign up to request clarification or add additional context in comments.

Comments

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.