1
我試圖使用shell_exec()
來獲取dig命令的輸出。shell_exec無法返回dig命令的輸出
這是我有:
<?php
header("Content-type: text/plain; charset=UTF-8");
echo shell_exec("dig google.com AAAA");
?>
如上所述,shell_exec()
無法返回命令的輸出:
$ 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
然而,當我與任何其他命令替換命令,東西很好地工作:
<?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()
不工作的命令,但對於其他命令正常工作;我怎樣才能使它工作?
編輯:中curl -v
作爲shell_exec("dig google.com AAAA")
的要求@choult輸出:
$ 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
當你運行-v捲曲時會發生什麼?例如。 curl -v http://localhost/test.php – choult
@choult,請參閱'curl -v'輸出的編輯問題。 – user2064000
雖然''echo shell_exec(「dig google.com AAAA 2> &1");'顯示任何東西?我會使用['exec()'](http://php.net/manual/en/function.exec.php)你可以給它傳遞一個包含所有輸出的變量,如果你給它一個returnvar它將包含退出狀態 –