Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > itunes and lsof

itunes and lsof
Thread Tools
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Apr 30, 2003, 05:16 AM
 
Hey all, Im really loving iTunes 4 and its library sharing abilities. I know that I can get info about how many users are connected to me from itunes sharing pane, but I'm looking for more detailed info, like who exactly is connected to me and what are they listening to. I started apropos'ing and have come up with this command to tell me who is connected

lsof -i TCP:3689

which tells me who is connected on port 3689, which i understand is the port used by itunes for sharing.

my questions are this:

how can i figure out what they are listening to?

does any other service use port 3689? and if so, should I the do a:

lsof -i TCP:3689 | grep iTunes

Im kinda thinking that a menubar status item would be nice that showed who was connected and what they were listening to. If I can get a little help in figuring out what file they are listening to, the rest will be cake.
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Apr 30, 2003, 06:36 AM
 
Ive been twiddling with my command and I've got it working pretty well (still dont know how to tell what song they are listening to)

here it is thus far:

lsof -nF n | grep "3689->"

This gives a list like this:

n24.207.218.212:3689->218.225.130.165:49163
n24.207.218.212:3689->68.160.205.102:29607
n24.207.218.212:3689->68.160.205.102:29532
n24.207.218.212:3689->218.217.71.14:49447
n24.207.218.212:3689->210.230.199.40:49832

How can I get just those characters in between -> and : to achieve output like this:

218.225.130.165
68.160.205.102
68.160.205.102
218.217.71.14
210.230.199.40

And then, how can I remove duplicates to get me to just:

218.225.130.165
68.160.205.102
218.217.71.14
210.230.199.40

Which is only the ip's of those connected to my iTunes library with duplicates removed.
     
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Apr 30, 2003, 07:16 AM
 
Here's a solution to the first part. Put the following in a text file, eg. ips.pl

Code:
foreach $line (<STDIN>) { if($line =~ /:3689->(.+):[0-9]+/) { # Found a match print "$1\n"; } }
Now assuming your pwd is the directory where this script is, do:

lsof -nF n | perl ips.pl

That should give you the output you want.

Wesley
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
Apr 30, 2003, 05:22 PM
 
The perl script above will do #1. The simple way to do #2 in Perl is to just store the IPs in a hash. So do the following:

Code:
#!/usr/bin/perl foreach $line (<STDIN> ) { if($line =~ /:3689->(.+):[0-9]+/) { # Found a match $ips{$1} = undef; } } print "$_" foreach (keys %ips);
Of course, I'm sure there's a way to do this just with a shell script, but perl is speedy enough. No need for the newline in the print statement btw, since you're not chomping the input.
(Last edited by itai195; Apr 30, 2003 at 05:29 PM. )
     
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Apr 30, 2003, 05:41 PM
 
Nothing really to add. But I'm frankly amazed that now that there is an iTunes thread here that we have an iTunes thread in every single forum.
     
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Apr 30, 2003, 08:51 PM
 
Originally posted by itai195:
Of course, I'm sure there's a way to do this just with a shell script, but perl is speedy enough. No need for the newline in the print statement btw, since you're not chomping the input.
The newline is needed (print "$_\n" foreach (keys %ips);) because the newline is removed since it isn't included in the match returned in $1. Originally I didn't have the newline in there and they all came out on one line...

Wesley
(Last edited by WJMoore; Apr 30, 2003 at 08:57 PM. )
     
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status: Offline
Reply With Quote
Apr 30, 2003, 09:02 PM
 
Originally posted by clarkgoble:
Nothing really to add. But I'm frankly amazed that now that there is an iTunes thread here that we have an iTunes thread in every single forum.
Especially the software forum. It's almost like "declare your major" day on a college campus... Everyone rushes and starts their own iTunes 4 thread... Ugh.

On a side note, I don't think that it is possible to see who is listening to what on your shared iTunes server. Wouldn't Apple have to have built this into iTunes' sharing protocol...? And since they most likely expect sharing to be for personal use (digital hub-typed stuff), and for that route, I doubt that they built this feature into iTunes for obvious reasons.
"In Nomine Patris, Et Fili, Et Spiritus Sancti"

     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
May 1, 2003, 01:35 AM
 
found this in another thread.

#!/bin/sh
# itunesshare
# Prints information about users browsing your iTunes library
# by Mithras (mithras at myrealbox.com)

# -- Settings --
# We guess at your music folder location
# If it is different, you should set it here
custom_music_location=""

# -----------

followalias() # routine to follow an alias
{
while [ $# -gt 0 ]; do
if [ -f "$1" -a ! -L "$1" ]; then
item_name=`basename "$1"`
item_parent=`dirname "$1"`
item_parent="`cd \"${item_parent}\" 2>/dev/null && pwd || echo \"${item_parent}\"`"
item_path="${item_parent}/${item_name}"
# nicer version thanks to clarkgoble
linksource=`osascript<<EOS
tell app "Finder"
set theItem to (POSIX file "${item_path}") as alias
if the kind of theItem is "alias" then
get the posix path of (original item of theItem as text)
end if
end tell
EOS`
echo "$linksource"
fi
shift
done
}

default_music_location="$HOME/Music"
if [ -z "$custom_music_location" ]; then
# no custom location; try following an alias
alias_music_location=`followalias "$default_music_location"`
if [ ! -z "$alias_music_location" ]; then
# not an alias; just use the regular location
music_location="$alias_music_location"
else
# follow the alias
music_location="$default_music_location"
fi
else
# user set a custom music location
music_location="$custom_music_location"
fi

browseusers=`/usr/sbin/netstat -f inet -W -n | awk '$4 ~ /3689/ { if ($3 != "33304") {print $5 } }' | sed -e 's/\.[^.]*$//g'`
streamusers=`/usr/sbin/netstat -f inet -W -n | awk '$4 ~ /3689/ { if ($3 == "33304") {print $5 } }' | sed -e 's/\.[^.]*$//g'`
browsecount=`echo "$browseusers" | wc -w | sed -e 's/ //g'`
streamcount=`echo "$streamusers" | wc -w | sed -e 's/ //g'`

echo "$browsecount users browsing, $streamcount listening."
echo " "
echo "--- Browsers ---"
for addr in `echo $browseusers`
do
nslookup "$addr" 2>&1 | awk '/Name/ {print $2} /Non-existent/ {print $5} '
done

echo " "
echo "--- Listeners ---"
for addr in `echo $streamusers`
do
nslookup "$addr" 2>&1 | awk '/Name/ {print $2} /Non-existent/ {print $5} '
done
echo " "

echo "--- Files ---"
for x in `fstat | awk '$2 ~ /iTunes/ && $8 == "r" && $9 ~ /Music/ {print $5}'`
do
find "$music_location" -inum $x 2>/dev/null
done

And it makes tasty output that meets all the requirements I originally had. Thanks y'all.
     
Junior Member
Join Date: Mar 2003
Status: Offline
Reply With Quote
May 1, 2003, 03:45 AM
 
and what about:


lsof -nF n | grep "3689->" | cut -d">" -f2 | awk -F":" '{ print $1}' | sort -u
     
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 1, 2003, 05:45 AM
 
Originally posted by bzImage:
and what about:


lsof -nF n | grep "3689->" | cut -d">" -f2 | awk -F":" '{ print $1}' | sort -u
Nice one! You have to love UNIX.
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:54 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2