我想使用(这些命令中的任何一个)从 PHP 内部检查某个隧道是否存在:
$(which lsof) -i -n | grep ssh
$(which netstat) -a | grep "localhost:ssh"
问题是,当我在 shell 中运行命令时,一切都很好,但是从 php 运行它们时,就像:
$reply = exec(CMD);
始终不返回任何内容。
有什么想法吗?
谢谢!
最佳答案
您可以将 stderr 重定向到 stdout 并获取 $output
和 $return_var
。为此,请像这样更改您的 exec() 调用:
exec('$(which lsof) -i -n | grep ssh 2>&1', $output, $return_var);
var_dump($return_var);
var_dump($output);
更多关于 exec 的信息:http://php.net/manual/en/function.exec.php (查看 $output
和 $return_var
参数)。
关于php - netstat 和 lsof 的执行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41182599/