sun, 30-apr-2006, 20:53

A couple months ago I got my first Apple since the Mac Classic I had in college. It's a MacBook Pro and so far I really like it. I've managed to get it to do almost everything my Linux laptop could do, but now I've got access to iTunes and Adobe's Creative Suite (although it's slow under Rosetta). If Apple would allow me to change the focus behavior, and implement the X11 cut and paste, it'd be the perfect system for a laptop.

On campus I have access to the iTunes playlists of all the people on the wireless network that are sharing their music library. And I have mine shared so other people can check out the artists I enjoy. Unfortunately, iTunes doesn't tell you what songs connected users are listening to or who is actually connected.

Since OS X is Unix, it's easy enough to examine the process tree and discover what network and filesystem connections iTunes is making. Running:

ps -axo 'pid command' | grep -v grep | grep 'iTunes ' | awk '{print $1}'

will show the process ID for iTunes. Once you have this number, you can use lsof -p [pid] to show all the files (and network connections, which are treated like files in Unix) that iTunes is using. Filtering the results by your iTunes library (grep /Users/$USER/Music/iTunes/iTunes Music/) yields the songs that are being played, both locally and over the network. And searching for ESTABLISHED shows the network connections. The last part of these lines show the IP addresses of the computers connected to you, and if there are two lines with the same destination IP address, that means they are actually playing from your music library.

To automate this, I wrote a Python script watch_itunes.py that automates this process. Note that this is a command-line tool, running from a terminal window. There are Dashboard widgets that are supposed to do this, but the one I tried didn't work, perhaps because I have an Intel mac.

To use the script: ./watch_itunes.py

By default, it will examine the process tree every 15 seconds, showing what's playing and who is connected or playing from your music library. Run it with -h to see a list of command line options.

Here's what it shows right now:

192.168.1.101 is connected but not listening to music
Portastatic                Bright Ideas               05 Little Fern.m4a

192.168.1.101 is listening to music
Arcade Fire                Funeral                    09 Rebellion (Lies).m4a
Portastatic                Bright Ideas               05 Little Fern.m4a

In the first two lines, I'm listening to Little Fern, and another computer is connected to my library, but isn't playing anything. In the second set of lines, they started listeing to Rebellion (Lies). The program will keep printing lines like these until you exit the program with Control-C.

tags: music  OS X  sysadmin 
Meta Photolog Archives