shell_exec 不能 return dig 命令的输出
shell_exec cannot return output of the dig command
我正在尝试使用 shell_exec()
获取 dig 命令的输出。
这是我的:
<?php
header("Content-type: text/plain; charset=UTF-8");
echo shell_exec("dig google.com AAAA");
?>
如上所述,shell_exec()
无法 return 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
命令不起作用,但对其他命令却正常;我怎样才能让它发挥作用?
编辑:@choult 为shell_exec("dig google.com AAAA")
:
请求的curl -v
的输出
$ 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
下面有没有给你看的?
echo shell_exec("dig google.com AAAA 2>&1");
不过我会使用 exec()
,你可以传递一个包含所有输出的变量,如果你给它一个 returnvar,它将包含退出状态。
我正在尝试使用 shell_exec()
获取 dig 命令的输出。
这是我的:
<?php
header("Content-type: text/plain; charset=UTF-8");
echo shell_exec("dig google.com AAAA");
?>
如上所述,shell_exec()
无法 return 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
命令不起作用,但对其他命令却正常;我怎样才能让它发挥作用?
编辑:@choult 为shell_exec("dig google.com AAAA")
:
curl -v
的输出
$ 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
下面有没有给你看的?
echo shell_exec("dig google.com AAAA 2>&1");
不过我会使用 exec()
,你可以传递一个包含所有输出的变量,如果你给它一个 returnvar,它将包含退出状态。