简体   繁体   中英

output of dns-sd browse command not redirected to file in busybox shell(ash)

To check if mdnsd is in probing mode we are using below command to browse for service and redirect its output a file, and hostname of the device is found in the command we decide that mdnsd is in probing mode.

command used for publishing service

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

To browse the service following command is used (Running in background)

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

To display the content of the file used cat.

cat /tmp/myfile

myfile is empty, if > replaced with tee , I see output on console myfile remains empty.

I am unable to understand what is going on.

Is there any pointer, help

EDIT
Just for completeness adding output, which i missed adding before.

# 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     ""

You appear to have a program with behavior that differs based on whether its output is to a TTY. One workaround is to use a tool such as unbuffer or script to simulate a TTY.

Moreover, inasmuch as the use of a file at all is done as a workaround, I suggest using a FIFO to actually capture the line you want without needing to write to a file and poll that file's contents.

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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