Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# create semi-random filenames for writing and reading to
# will probably only work on mac os x leopard and snow leopard
tempfoo=`basename $0`
TFILE=`mktemp /tmp/XXXXXXXX` || exit 1
TFILE2=`mktemp /tmp/2XXXXXXXX` || exit 1
# grabs browseable list of bonjour shared libraries
dns-sd -B _daap._tcp 1>$TFILE &
# grab the process identifiers for the dns-sd process WHICH NEVER STOPS
TFILE_PID=$!
# dont remove this, otherwise script will not collect the input needed. need to find a way to make sure file has been created
sleep 1
# takes the last library in the list and formats it for dns-sd -L
res=$(tail -n1 $TFILE | awk '{gsub(/[ ]+/," ")}1' | cut -d' ' -s -f6-)
# read the protocol and then name in, allows us to use different protocols in the future
read -r protocol name <<< "$res"
# look up specific libraries
dns-sd -L "$name" "$protocol" 1>$TFILE2 &
# grab the process identifiers for the dns-sd process WHICH NEVER STOPS
TFILE2_PID=$!
# we have to wait a second because dns-sd -L can take a while to respond
sleep 1
res2=$(awk '{gsub(/[ ]+/," ")}1' $TFILE2)
# for some reason cut wasnt respecting the spaces, but in another var it worked fine
ip=$(echo $res2 | cut -d' ' -f10| cut -d'.' -f1-2)
if [[ "$ip" == "" ]]
then echo "No computers are sharing their libraries"
else ping $ip
fi
# kill the dns-sd processes
# remove the temporary files
kill $TFILE_PID
kill $TFILE2_PID
rm $TFILE
rm $TFILE2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment