Jump to content

exec() does not always show output


Recommended Posts

Hello,

I can't get exec() working with a binary, but it works with whoami, the binary also outputs on a plain linux machine. I have error reporting E_ALL enabled but i can't find anything usefull in the logs. What can i do?

Thanks for helping.

See pic for the output of the script, whoami works but swetest gives nothing. I know it works, it is in use elsewhere on servers.

<?php
if (ini_get('safe_mode'))
  echo 'safe mode is on<br>';
else
  echo 'safe mode is off<br>';
if (function_exists('exec'))
  echo "exec is enabled<br>";
else
  echo "exec is disabled<br>";
echo exec('whoami');
echo exec('swetest -h');
?>

Untitled.png

Edited by JeanCremers
Link to comment
Share on other sites

If you look at the manual page for exec you'll see that it returns just

Quote

The last line from the result of the command.

If you look at the arguments list, you'll see there are two additional arguments you can pass to exec

Quote

output

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

return_var

If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

So if you want  the full output, you need to pass a variable as the second argument and it will be filled with the output.

exec('swetest -h', $output);
var_dump($output);

 

Link to comment
Share on other sites

Then you need to check a few things.

  1. Use the third parameter to get the exit-code and see what it is.  Typically 0 would mean success, anything else would be an error.
  2. There are two ways a program can output text, either via STDOUT, or STDERR.  By default only STDOUT is captured.  To capture STDERR as well you need to redirect it to STDOUT.  This is done by adding 2>&1 to the end of your command.
  3. It's generally best to use a full path to the executable as your script may be running with a different $PATH value than you're normal shell.

 

Link to comment
Share on other sites

Thanks!

I get error 127, path not found indeed.

I tried,

	exec('swetest');
	exec('./swetest');
	exec('/public_html/se/swetest');
	

Neither works.

ps. i see google is full of exec error 127 :(

If i dump it all in the server root i still get 127 so something must be fishy in the path?

Edited by JeanCremers
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.