Viewed   74 times

I'm writing a PHP script to be used to check for network connections with Linux shell command ping calling it with PHP's exec():

<?php


// Bad IP domain for testing.
$domain_bad = "lksjdflksjdf.com";

$ip_address = $domain_bad;

exec("ping -c 1 $domain_bad", $output, $return_var);

var_dump($return_var);
echo "return_var is: $return_var" . "n";
var_dump($output);


exit;
?>

I'm not getting the output for the error message from ping in $output which is what I'm expecting:

$ php try.php
ping: unknown host lksjdflksjdf.com
int(2)
return_var is: 2
array(0) {
}

If the domain is a good domain, such as yahoo.com, then $output has the output from ping in an array. But if it's an error such as 'ping: unknown host lksjdflksjdf.com' it doesn't get returned to the $output array.

Why is this happening and is there a better method to do this?

 Answers

5

You should redirect stderr to stdout.

To do that, change your exec() call like this:

exec("ping -c 1 $domain_bad 2>&1", $output, $return_var);

More info about 2>&1 meaning here.

Wednesday, August 10, 2022
2

Problem solved with the following command:

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:wampbinphpphpVERSIONNUMBERphp-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);
Friday, December 9, 2022
4

Does your computer have UAC turned off? (Windows 7 or Vista, accessing C: with out UAC perms will error so for ease turn it off),

Secondly, exec requires a string input,

Thirdly need to be escaped as Phill said

exec('java -jar "C:/batik/batik-rasterizer.jar" -m C:/pathto/image/png -d 
"C:/pathtoimg/temp/2fa8af078803491746235057c546c1b6.png" -w 800 
"C:/pathtoimg/temp/2fa8af078803491746235057c546c1b6.svg"');

UPDATED FOR COMMENT ON PHILLS

Check that your allowed to use exec/system/backticks, using the CLI

  1. Create a php file with <?php exec("echo hello"); ?> inside it save it
  2. Then start CMD,
  3. open a new window (open up my/ computer) and browse to your php.exe application
  4. copy the address of the folder where you found it
  5. in cmd type "cd " without quotes and after that right click and click pase then hit enter
  6. type "php " (again without quotes) drag php file saved to the cmd box and drop it
  7. then hit enter and see if it says hello in the cmd box

if this does not work check java is working without path to exe, in the cmd windows type your java --help and see if it works

Tuesday, August 2, 2022
1

this is just a guess; I've written years ago a very wobbly chat script that used output buffering as well (and fetching new messages in while(true) loop) ..

While doing this I've encountered the same problems that sometimes the script stalled (blank screen), sometimes it took a while until the characters appeared and additionally this was also browser specific.

Here are the relevant code snippets I've added to the script to have it work with IE6 and FF2 (as I said, years ago ...)

<?php
    // End output buffering
    ob_end_flush(); 

    // IE and Safari Workaround
    // They will only display the webpage if it's completely loaded or
    // at least 5000 bytes have been "printed".
    for($i=0;$i<5000;$i++)
    {
        echo ' ';
    }

    while( ... )
    {
        echo 'Message';
        ob_flush();
        flush();
    }
?>

It worked for me, so maybe you could give it a try as well. (Altough I have no idea how modern browsers and server infrastrucutre will behave to this).

Sunday, December 4, 2022
 
4

shell_exec returns all of the output stream as a string. exec returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.

See

  • http://php.net/manual/en/function.shell-exec.php
  • http://php.net/manual/en/function.exec.php
Friday, October 14, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :