0

I am trying to write a php script which can scan the network for any mDNS records, and return the results.

'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.

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.

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.

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.

You can see here when i run the command in terminal, i can get a response almost immediately.

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?

Jake6192
  • 13
  • 5
  • Your comment on not terminating, that would be exactly the reason. You also never mentioned what actually happens when you run your php script (I assume it hangs?) – JensV Apr 02 '19 at 14:25
  • Yeah sorry, the script does just hang and I'm left with no response. Do you know of any way i can force it to terminate? – Jake6192 Apr 02 '19 at 14:27
  • Are you expecting the command to terminate on it's own? – JensV Apr 02 '19 at 14:31
  • This could help perhaps: https://stackoverflow.com/a/6144213/2232127 – JensV Apr 02 '19 at 14:35
  • Yes I would like it to - I need to get the results from it.. Whether thats a method that I can call 'x seconds' after executing, to terminate and return the result. Or maybe there is something else i can do? – Jake6192 Apr 02 '19 at 14:35
  • With the linked answer, combined with [proc_close](https://www.php.net/manual/en/function.proc-terminate.php) you should be able to accomplish this – JensV Apr 02 '19 at 14:36
  • No I have tried this - the script is still hanging on the proc_open call – Jake6192 Apr 02 '19 at 14:39
  • Are you sure, my local test doesn't block at all at the proc_open call – JensV Apr 02 '19 at 14:43
  • Yeah, I'm certain. Are you testing with the dns-sd command? I have a feeling that it may behave slightly differently to more typical ones. – Jake6192 Apr 02 '19 at 14:46
  • Perhaps https://stackoverflow.com/a/7149229/2232127 will help you understand it a bit better – JensV Apr 02 '19 at 15:02
  • Perfect, managed to get things working with this. Appreciate the help @JensV – Jake6192 Apr 02 '19 at 15:12

1 Answers1

0

I managed to get this working, thanks to @JensV for pointing me to this answer - 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);
?>
Jake6192
  • 13
  • 5