View open sockets in Transmission or other apps

Feb 16, '06 05:35:00AM

Contributed by: Intenzity

I like the Torrent client Transmission, but right now it does not have the same level of detail as some other clients do (BitTorrent or Azureus) such as showing the IPs of the peers you are connected to. So I wrote a script, and I have GeekTool run it every five seconds and then I put the results in a window on my desktop. The result of the script is a list of open sockets, and a title at the top that tells me how many Transmission is using.

Here's the code:

ThePID=`ps -U yourusernamehere | grep TRANSMISSION | grep -v grep | awk '{ print $1 }'`; 
TheFiles=`fstat -p $ThePID | grep stream | wc -l`; 
echo $TheFiles " Transmission files open"; 
netstat -d -n -f inet | grep "ESTABLISHED" | grep -v 127 | awk '{print $5}'

Substitute your user name and the name of the application you want to monitor above, but it does need to be the name exactly as it appears in the ps command.

A brief explanation of what is going on here. The first part of the script determines the current PID of the Transmission client, but you could grep for any application name, such as a browser if you wanted to monitor its active connections.

The remaining lines use the PID of the application that comes back to narrow the search down to just the sockets that app is using, and also remove the local host connections and then print out the IPs and ports with a total.

Enjoy, feedback welcome.

Comments (7)

Mac OS X Hints