shell_exec无法返回dig命令的输出

时间:2016-03-02 15:28:28

标签: php shell-exec

我试图用<div class="dropdown"> <select class="selectpicker" data-width="100%" data-live-search="true" style="border-radius: 0px;"> {% for location in locations %} <optgroup label="{{ location.asLocation }}"> {% for building in buildings %} {% if building.ixLocation == location.ixLocation %} <option>#{{building.iBuildingNum}} - {{building.asBuildingName}}</option> {% endif %} {% endfor %} </optgroup> {% endfor %} </select> </div> 来获取dig命令的输出。

这就是我所拥有的:

shell_exec()

如上所述,<?php header("Content-type: text/plain; charset=UTF-8"); echo shell_exec("dig google.com AAAA"); ?> 无法返回shell_exec()命令的输出:

dig

虽然命令本身工作正常:

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

重定向也能按预期工作:

$ 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

但是,当我用任何其他命令替换$ 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 命令时,事情就完美了:

dig

为什么<?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 不能用于shell_exec()命令,而是为其他命令正常工作;我怎样才能让它发挥作用?

修改:@ dig根据curl -v的要求输出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

1 个答案:

答案 0 :(得分:3)

以下是否显示您的任何内容?

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

我会使用exec(),你可以传递一个包含所有输出的变量,如果你给它一个returnvar,它将包含退出状态。