简体   繁体   English

如何在php shell_exec中使用'dns-sd'命令?

[英]How do I use the 'dns-sd' command in a php shell_exec?

I am trying to write a php script which can scan the network for any mDNS records, and return the results. 我正在尝试编写一个php脚本,可以扫描网络中的任何mDNS记录,并返回结果。

'dns-sd -B _roomcast-capi._tcp'

I am working on OSx - from what I understand this command does not work on windows without installing extra SW. 我正在研究OSx - 据我所知,这个命令不能在没有安装额外SW的情况下在Windows上运行。

The problem is that this unix command is not working with shell_exec or anything similar.. The PHP just hangs, and I'm left waiting for a non-existent response. 问题是这个unix命令不能与shell_exec或类似的东西一起使用.. PHP只是挂起,我等待一个不存在的响应。

I have tried running the command through shell_exec(), exec(), system(), proc_open() & passthru() - no idea what they do differently, but they all have the same result.. I have also tried redirecting the STDERR output and that didn't seem to make a difference. 我试过通过shell_exec(),exec(),system(),proc_open()和passthru()运行命令 - 不知道他们做了什么不同,但他们都有相同的结果..我也尝试重定向STDERR输出,这似乎没有什么区别。

One of my attempts.. 我的一个尝试..

  $p = shell_exec("dns-sd -B _roomcast-capi._tcp 2>&1");
  echo($p);

Another attempt... 另一种尝试......

  $descspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "error-output.txt", "a")
  );
  $process = proc_open('dns-sd -B _roomcast-capi._tcp', $descspec, $pipes);
  foreach($pipes as $pipe) {
    var_dump($pipe);
    // echo "$process<br />";
  }

I could list the rest of my attempts here but they're probably all wrong if I'm honest.. PHP is not my strongest area. 我可以在这里列出其余的尝试,但如果我说实话,他们可能都错了.PHP不是我最强的领域。

You can see here when i run the command in terminal, i can get a response almost immediately. 你可以看到我在终端运行命令时,我几乎可以立即得到响应。

https://prnt.sc/n6fe9e https://prnt.sc/n6fe9e

I have noticed that when i run this command in the terminal on my mac i have to terminate the response with Ctrl+C - could this be the reason for shell_exec not terminating? 我注意到,当我在我的mac终端上运行此命令时,我必须使用Ctrl + C终止响应 - 这可能是shell_exec无法终止的原因吗?

I managed to get this working, thanks to @JensV for pointing me to this answer - https://stackoverflow.com/a/7149229/4357255 我设法让这个工作,感谢@JensV指点我这个答案 - https://stackoverflow.com/a/7149229/4357255

<?php
  $proc = proc_open('dns-sd -B _roomcast-capi._tcp', array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes);
  echo fread($pipes[1], 999999);
  $proc_status=proc_get_status($proc);
  $pid=trim(exec('ps h -o pid  --ppid '.$proc_status['pid']));
  exec('kill -s 9 '.$proc_status['pid']);
  exec('gdb -p '.$pid.' --batch -x /usr/share/gdb_null_descr');
  array_map('fclose',$pipes);
  proc_close($proc);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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