Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 11-03-2005, 06:16 AM   #1
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
Parsing Output of mDNS, AppleScript Studio

My program is looking for computers that have Remote Apple Events enabled. It's possible to get this list via Bonjour/Rendezvous by executing this command:

mDNS -B _eppc._tcp local

... and then, from another terminal...

killall mDNS

(to stop the process).

All I really want is the information that comes up in the "Instance Name" column. Is there any way to do this using grep or something like it? Or even directly through AppleScript Studio? (Note you need Remote Apple Events enabled on at least one of the computers on your network to test this).

Any suggestions?

-Matt
MattPat is offline   Reply With Quote
Old 11-03-2005, 08:47 AM   #2
guardian34
All Star
 
Join Date: Jul 2004
Posts: 713
Would you post an example of mDNS' output? (I don't have a second Mac anymore.)
guardian34 is offline   Reply With Quote
Old 11-03-2005, 03:47 PM   #3
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
Output of: mDNS -B _eppc._tcp local
(enclosed in code block to preserve whitespace)
Code:
Browsing for _eppc._tcplocal
Talking to DNS SD Daemon at Mach port 3843
Timestamp     A/R Flags Domain                   Service Type             Instance Name
15:44:18.030  Add     1 local.                   _eppc._tcp.              MATTMAC
15:44:18.033  Add     0 local.                   _eppc._tcp.              MACLAP
I somehow want to be able to get simply the list:
MATTMAC
MACLAP

-Matt

Last edited by MattPat; 11-03-2005 at 03:49 PM.
MattPat is offline   Reply With Quote
Old 11-03-2005, 04:05 PM   #4
guardian34
All Star
 
Join Date: Jul 2004
Posts: 713
I would try something like this:
Code:
do shell script "mDNS -B _eppc._tcplocal | colrm 1 74"
set hostList to paragraphs 4 through -1 of result
guardian34 is offline   Reply With Quote
Old 11-03-2005, 04:14 PM   #5
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
This almost works perfectly.

The only problem: I have to manually kill off mDNS to get it to complete (it doesn't exit after printing output, it stays open and listens for more connections). I tried putting a "delay 1" followed by "killall mDNS" in the script, but AppleScript won't work like that... it wants to wait for the first script to complete before moving on.

Any suggestions?

-Matt
MattPat is offline   Reply With Quote
Old 11-03-2005, 05:25 PM   #6
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
I have no experience with this, but I've seen that there is a "with timeout" clause available in AppleScript - maybe that would help.
hayne is offline   Reply With Quote
Old 11-03-2005, 05:28 PM   #7
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
Tried it... with timeout only applies when you're referencing an application. It has no effect on other AppleScript commands

-Matt
MattPat is offline   Reply With Quote
Old 11-03-2005, 06:46 PM   #8
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
One thing you could do is run the 'mDNS' in the background (outputting to a file) - that way your "do shell script" would return immediately and you could just do a 'delay 20' (or whatever) in your AppleScript and then do the 'killall'.

You start commands in the background in a shell script by appending a '&' at the end of the command.
You redirect the text that usually goes to the terminal into a file by using '>' followed by the name of the file you want to write to.

mDNS -B _eppc._tcplocal | colrm 1 74 > /tmp/myDnsOutput.txt &
hayne is offline   Reply With Quote
Old 11-04-2005, 11:42 AM   #9
Raven
Hall of Famer
 
Join Date: Jul 2003
Location: Montreal
Posts: 4,782
I've posted a similar question in another thread not knowing there was one here... However I'm joining this one since its gone much further up to now. My only "question" even tough its nor realy one is: Since I need to have some output back from the command, but I do need to have it stop as it is the case for MattPat... So if I were to use the last command line Hayne suggested, the I could have a "proper" output to text file... Then would make more sense to get the info back form the text file in the script so its getting the info from a static file and not an interactive one like mDNS itself ?
__________________
Waffled foreheads are a symptom of broken keyboards and inexperienced users
Raven is offline   Reply With Quote
Old 11-10-2005, 09:20 PM   #10
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
Well, that solution didn't quite work for me, but I was able to get it working: I wrote a shell script which I put in my application bundle that returns the same output as the above command, but terminates itself after 1 second.

Code:
#!/bin/bash

mDNS -B _eppc._tcp local | colrm 1 74 &
sleep 1
killall mDNS
It works perfectly for me Thanks for all your help, everyone!

-Matt
MattPat is offline   Reply With Quote
Old 11-16-2005, 06:26 AM   #11
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
Even better than my previously-posted example: you can put that shell script right into your AppleScript:

Code:
on getmDNS()
   set theScript to "mDNS -B _eppc._tcp local | colrm 1 74 &
sleep 1
killall mDNS"

   set hostList to paragraphs 4 thru -1 of (do shell script theScript)
   return hostList
end getmDNS
Now, call "getmDNS()" from anywhere in your AppleScript to get a list of all the hosts with this service.

Code:
set theHosts to getmDNS()
I did this because my program was failing if run from within its disk image because it couldn't properly get the bundle's Resources path. This way, there is no need to deal with extra files

-Matt
MattPat is offline   Reply With Quote
Old 11-17-2005, 01:52 PM   #12
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
The man file for mDNS says this:

Quote:
Originally Posted by man mDNS
If you wish to perform DNS Service Discovery operations from a scripting language, then the best way to do this is not to execute the mDNS command and then attempt to decipher the textual output, but instead to directly call the DNS-SD APIs using a binding for your chosen language.

For example, if you are programming in Ruby, then you can directly call DNS-SD APIs using the dnssd package documented at <http://rubyforge.org/projects/dnssd/>. Similar bindings for other languages are also in development.

I can't see any new packages in other languages, but assuming better Ruby skills than mine, it ought to be possible to call Ruby from Applescript.
bramley is offline   Reply With Quote
Old 11-17-2005, 03:41 PM   #13
MattPat
Prospect
 
Join Date: Sep 2005
Posts: 33
Well, it's true that this isn't the best way to do it... but it works, and there isn't an AppleScript binding that I (or anyone, apparently) am aware of. If I find anything new, I'll post it here, but for now this seems to accomplish the job.

-Matt
MattPat is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:59 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.