0

I've a program which must be runned from cmd.exe and be supplid a few parameters. The command looks like this:

query.exe USERNAME PASSWORD WMI_QUERY MACHINE

This program is working fine. But when I try to run it from PHP with the following code:

function execute_query($ip, $username, $password, $query){

    $runCMD ="query.exe " . $username . " " . $password .  ' "' .$query . '" '  . $ip;
    echo exec($runCMD);
    print_r ($stdout);
}

I don't get any output. It says that the process terminated successfully, but nothing else, although the program runs and returns the output successfully. I'm using windows and XAMPP with php 7.1 I tired using shell_exec, but didn't have any luck. Any other ideas?

5
  • 1
    exec() returns output in an array for each line of output in the terminal. iirc shell_exec does not return anything. check the array param that is passed by reference - php.net/manual/en/function.exec.php Commented Nov 7, 2017 at 13:41
  • also $stdout is not defined Commented Nov 7, 2017 at 13:42
  • woops, ignore my first comment, i don't know why I thought shell_exec doesn't return anything, but $stdout is still not defined Commented Nov 7, 2017 at 13:43
  • You're not actually executing your $runCMD. Commented Nov 7, 2017 at 13:56
  • It didn't run $runCMD because I was testing.I tried creating var $output = [] and set it as second argument, but it was empty when I printed it. Commented Nov 7, 2017 at 14:10

1 Answer 1

1

I usually simply use:

passthru("the_command 2>&1");

the 2>&1 bit is to redirect STDERR to STDOUT, which is where your output probably is, when the command fails...

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

1 Comment

also, to capture the output you can use ob_start() and ob_get_contents()

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.