0

为了检查 mdnsd 是否处于探测模式,我们使用以下命令来浏览服务并将其输出重定向到一个文件,并且在我们决定 mdnsd 处于探测模式的命令中找到设备的主机名。

用于发布服务的命令

dns-sd -R "Test status" "_mytest._tcp." "local." "22"

使用以下命令浏览服务(在后台运行)

dns-sd -lo -Z _mytest._tcp > /tmp/myfile &

使用 cat 来显示文件的内容。

cat /tmp/myfile

myfile 为空,如果>替换为tee,我看到控制台 myfile 上的输出仍然为空。

我无法理解发生了什么。

有没有指点,求助

编辑
只是为了完整性添加输出,我之前错过了添加。

# dns-sd -lo -Z _mytest._tcp local
Using LocalOnly
Using interface -1
Browsing for _mytest._tcp
DATE: ---Tue 25 Apr 2017---
11:09:24.775  ...STARTING...

; To direct clients to browse a different domain, substitute that domain in place of '@'
lb._dns-sd._udp                                 PTR     @

; In the list of services below, the SRV records will typically reference dot-local Multicast DNS names.
; When transferring this zone file data to your unicast DNS server, you'll need to replace those dot-local
; names with the correct fully-qualified (unicast) domain name of the target host offering the service.

_mytest._tcp                                    PTR     Test\032status._mytest._tcp
Test\032status._mytest._tcp                     SRV     0 0 22 DevBoard.local. ; Replace with unicast FQDN of target host
Test\032status._mytest._tcp                     TXT     ""
4

1 回答 1

1

您似乎有一个程序,其行为取决于其输出是否为 TTY。一种解决方法是使用诸如unbufferscript模拟 TTY 之类的工具。

此外,由于文件的使用完全是一种解决方法,我建议使用 FIFO 来实际捕获您想要的行,而无需写入文件并轮询该文件的内容。

#!/bin/sh

newline='
'

# Let's define some helpers...
cleanup() {
  [ -e /proc/self/fd/3 ] && exec 3<&-                   ## close FD 3 if it's open
  rm -f "fifo.$$"                                       ## delete the FIFO from disk
  if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then  ## if our pid is still running...
    kill "$pid"                                         ## ...then shut it down.
  fi
}
die() { cleanup; echo "$*" >&2; exit 1; }

# Create a FIFO, and start `dns-sd` in the background *thinking* it's writing to a TTY
# but actually writing to that FIFO 
mkfifo "fifo.$$"
script -q -f -c 'dns-sd -lo -Z _mytest._tcp local' /proc/self/fd/1 |
  tr -d '\r' >"fifo.$$" & pid=$!

exec 3<"fifo.$$"

while read -t 1 -r line <&3; do
  case $line in
    "Script started on"*|";"*|"")  continue;;
    "Using "*|DATE:*|[[:digit:]]*) continue;;
    *)                             result="${result}${line}${newline}"; break
  esac
done

if [ -z "$result" ]; then
  die "Timeout before receiving a non-boilerplate line"
fi

printf '%s\n' "Retrieved a result:" "$result"
cleanup
于 2017-04-25T13:06:28.480 回答