#!/bin/sh # The following directory should exist, be initially empty, and can be anywhere. LOG_DIR=~/logs/iTunes Users # The file name of the log will contain the current count (if any). # Note: I'm assuming that this folder is empty except for the log file. COUNT=`ls "$LOG_DIR"` if [ "$COUNT" = "" ] ; then COUNT=0 fi # Now parse netstat looking for established connections on port 3689. NEW_COUNT=`/usr/sbin/netstat -an | sed -ne 's/.* [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.3689 {1,}([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}).*ESTABLISHED$/1/p' | # Sort the ip numbers so we can remove any duplicates. sort | # Remove duplicates. uniq | # Save a copy in the log directory. # Notice that the filename is the actual client count. tee "$LOG_DIR/$COUNT" | # Now count the current connections. wc -l | # This last sed command strips any leading spaces. sed 's/ *//'` # Now see if the count has changed. If so, rename the log file with the new count # so it can be seen in the Finder. if [ "$COUNT" != "$NEW_COUNT" ] ; then # Rename the log to reflect the current count in the Finder window. mv "$LOG_DIR/$COUNT" "$LOG_DIR/$NEW_COUNT" # This will refresh the Finder window for 10.3 users. open "$LOG_DIR" fi