-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzeroconf-ssh-host
More file actions
executable file
·93 lines (83 loc) · 2.98 KB
/
zeroconf-ssh-host
File metadata and controls
executable file
·93 lines (83 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
#
# A utility to query multicast DNS for a named host or all hosts. This
# is used by zssh to find all hosts in the local domain that advertise
# ssh
#
if [[ "$1" = "-v" ]]; then
VERBOSE=true
shift
fi
if [[ -n "$1" ]]; then
HOST="$1"
else
HOST=ANYHOST
fi
# conditional printf
function vftrace() {
if [[ -n "$VERBOSE" ]]; then
printf "$@"
fi
}
TIMEOUT1=5
TIMEOUT2=1
function initfile() {
echo > $1
}
vftrace "Looking up %s\n" "\"${HOST}\""
# We use a couple of temporary files. Open them now so we can simply
# append all results
FILE=/tmp/zeroconf-hosts.txt
initfile $FILE
FILE2=/tmp/zeroconf-addresses.txt
initfile $FILE2
vftrace "Finding ssh advertisers..."
###############################################################################
# the following performs "browse Multicast DNS for hosts advertising
# ssh until $TIMEOUT1 piping results to a file, then quit and clean up"
( dns-sd -B _ssh._tcp . >> $FILE ) & pid=$!
( sleep $TIMEOUT1 && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher
###############################################################################
vftrace "done\n"
while read TIMESTAMP AR FLAGS IF DOMAIN SERVICE NAME; do
if [[ "$DOMAIN" = "local." ]]; then
shopt -s nocasematch
# For Apple machines, the names Multicast DNS browsing
# return are not suitable for lookup, we need to "crunch"
# them the way apple does - replace spaces with hyphens
# and removing apostrophes.
#
# Note the apostrophe character Apple adds to mac names is
# "single quote open" which you can type using Option-]
ADJUSTED_NAME=`echo $NAME | tr ' ' '-' | tr -d "’"`
vftrace "Compare to %30s ..." "\"${ADJUSTED_NAME}\""
if [[ ${ADJUSTED_NAME} =~ ${HOST} || $HOST = ANYHOST ]] ; then
vftrace " found!\n"
if [[ $SERVICE == *ssh* ]]; then
vftrace "ssh is exported by %s\n" "${NAME}"
vftrace "ADJUSTED_NAME $ADJUSTED_NAME\n"
else
vftrace "ssh is NOT exported" ""
ADJUSTED_NAME=NONE
fi
if [[ ! $ADJUSTED_NAME = NONE ]]; then
vftrace "Looking up the IPV4 address of %s in ZeroConf..." "\"${ADJUSTED_NAME}\""
###############################################################
# the following performs "lookup $ADJUSTED_NAME in
# Multicast until $TIMEOUT2 then quit and clean up"
( dns-sd -G v4 "${ADJUSTED_NAME}.local" >> $FILE2 ) & pid=$!
( sleep $TIMEOUT2 && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher
###############################################################
vftrace " done\n"
if [[ ! ${HOST} = ANYHOST ]]; then
break
fi
fi
else
vftrace " no match\n" ""
fi
fi
done < $FILE
cat $FILE2 | grep .local. | sort -k6 -u