简体   繁体   中英

shell_exec cannot return output of the dig command

I'm trying to get the output of the dig command with shell_exec() .

This is what I have:

<?php
header("Content-type: text/plain; charset=UTF-8");
echo shell_exec("dig google.com AAAA");
?>

As stated above, shell_exec() is unable to return the output of the dig command:

$ curl http://localhost/test.php
(no output)

although the command itself works fine:

$ dig google.com AAAA

; <<>> DiG 9.9.5-3ubuntu0.7-Ubuntu <<>> google.com AAAA
[...]

;; ANSWER SECTION:
google.com.     229 IN  AAAA    2404:6800:4007:805::200e
[...]

;; MSG SIZE  rcvd: 67

Redirecting it works as expected, too:

$ dig google.com AAAA > ~/f1.txt
$ cat ~/f1.txt

; <<>> DiG 9.9.5-3ubuntu0.7-Ubuntu <<>> google.com AAAA
[...]

;; ANSWER SECTION:
google.com.     229 IN  AAAA    2404:6800:4007:805::200e
[...]

;; MSG SIZE  rcvd: 67

However, when I replace the dig command with any other command, things work perfectly:

<?php
header("Content-type: text/plain; charset=UTF-8");
echo shell_exec("uname -a");
?>

$ curl http://localhost/test.php
Linux lubuntu0 3.19.0-33-generic #38~14.04.1-Ubuntu SMP Fri Nov 6 18:17:49 UTC 2015 i686 i686 i686 GNU/Linux

Why is shell_exec() not working for the dig command but working normally for the other commands; and how can I make it work?


Edit : The output of curl -v as requested by @choult for shell_exec("dig google.com AAAA") :

$ curl -v http://localhost/test.php
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /test.php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 02 Mar 2016 15:43:01 GMT
* Server Apache/2.4.18 (Unix) PHP/7.0.3 mod_perl/2.0.8-dev Perl/v5.16.3 is not blacklisted
< Server: Apache/2.4.18 (Unix) PHP/7.0.3 mod_perl/2.0.8-dev Perl/v5.16.3
< X-Powered-By: PHP/7.0.3
< Content-Length: 0
< Content-Type: text/plain; charset=UTF-8
< 
* Connection #0 to host localhost left intact

Does the following show you anyting?

echo shell_exec("dig google.com AAAA 2>&1"); 

I would use exec() though, you can pass it a variable that will contain all the output and if you give it a returnvar it will contain the exit status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM