Finding open network sockets on Mac OSX

I saw that there is a version of Leopard hanging around on the internet that can be used to run it on any Intel PC (well… an Intel PC with certain specifications!).

To find out if the the proof indeed lies in the pudding, I started to download it via a torrent. Now, downloading a torrent that is over 2 gigs is not a simple task — as least not here down in New Zealand, where bandwidth can be be at best described as quite sad.

So my torrent download has now been going on for a day and a half, and I’ve been a good boy — I’ve seeded more than downloaded 🙂

However, I wanted to find out all the open network sockets so that I could possibly open up my firewall or tweak it somehow to increase the speed of downloads. So I needed to find out the all the open network sockets to see the port number.

First I tried the usual netstat which gave me the following output:


Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
tcp4       0      0  192.168.0.102.55129    74-46-59-218.dr0.13704 ESTABLISHED
tcp4       0      0  192.168.0.102.55105    host66-227-dynam.53868 ESTABLISHED
tcp4       0      0  192.168.0.102.54980    82.152.9.81.21695      ESTABLISHED
tcp4       0      0  192.168.0.102.54723    73.167.217.87.dy.4630  ESTABLISHED
tcp4       0      0  192.168.0.102.54418    163-158.static.q.http  CLOSE_WAIT
tcp4       0  34789  192.168.0.102.54397    71-80-169-111.dh.16262 ESTABLISHED
tcp4       0      0  192.168.0.102.54387    213.22.163.110.6887    ESTABLISHED
tcp4       0      0  192.168.0.102.54380    80.254.74.63.29113     ESTABLISHED
tcp4       0      0  192.168.0.102.53414    20129006121.user.6881  ESTABLISHED
tcp4       0      0  192.168.0.102.53183    adsl-76-248-156-.51413 ESTABLISHED
tcp4       0      0  192.168.0.102.52146    cpe-24-162-115-1.53692 ESTABLISHED

... blah ...       

However the netstat out put does not show the process id and the name of actual name of the process.

Then I found (thanks to the Man pages and Google), that there is a better way to do this — lsof -i. This gives you an output like:


i-n-d-i-v-e-r:~ indy$ sudo lsof -i
COMMAND    PID           USER   FD   TYPE    DEVICE SIZE/OFF   NODE NAME
launchd      1           root   13u  IPv6 0x3c2bbe8      0t0    TCP localhost:631 (LISTEN)
launchd      1           root   14u  IPv4 0x3f21e64      0t0    TCP localhost:631 (LISTEN)
mDNSRespo   21 _mdnsresponder    7u  IPv4 0x3c28b18      0t0    UDP *:5353
mDNSRespo   21 _mdnsresponder    8u  IPv6 0x3c28a40      0t0    UDP *:5353
configd     27           root    8u  IPv4 0x3c28cc8      0t0    UDP *:*
configd     27           root   12u  IPv6 0x45b8da0      0t0 ICMPV6 *:*
mysqld     103         _mysql   10u  IPv4 0x3f21a68      0t0    TCP *:3306 (LISTEN)
ntpd      3903           root   20u  IPv4 0x3c282a8      0t0    UDP *:123
ntpd      3903           root   21u  IPv6 0x3c28608      0t0    UDP *:123
ntpd      3903           root   22u  IPv6 0x3c28da0      0t0    UDP localhost:123
ntpd      3903           root   23u  IPv4 0x3c28968      0t0    UDP localhost:123
ntpd      3903           root   24u  IPv6 0x3c28890      0t0    UDP localhost:123
ntpd      3903           root   25u  IPv4 0x3c26a30      0t0    UDP 192.168.0.102:123
ntpd      3903           root   27u  IPv6 0x3c27b10      0t0    UDP i-n-d-i-v-e-r.local:123
SystemUIS 4038           indy    9u  IPv4 0x3c287b8      0t0    UDP *:*
GrowlHelp 4047           indy    3u  IPv4 0x4ea3a68      0t0    TCP 192.168.0.102:63110->216.64.142.80:80 (CLOSE_WAIT)
Skype     4048           indy    6u  IPv4 0x3c26370      0t0    UDP localhost:55598
Skype     4048           indy   32u  IPv4 0x3db9e64      0t0    TCP 192.168.0.102:51804->c211-30-0-59.wavrl1.nsw.optusnet.com.au:443 (ESTABLISHED)
i-n-d-i-v-e-r:~ indy$ sudo lsof -i -P
COMMAND    PID           USER   FD   TYPE    DEVICE SIZE/OFF   NODE NAME
launchd      1           root   13u  IPv6 0x3c2bbe8      0t0    TCP localhost:631 (LISTEN)
launchd      1           root   14u  IPv4 0x3f21e64      0t0    TCP localhost:631 (LISTEN)
mDNSRespo   21 _mdnsresponder    7u  IPv4 0x3c28b18      0t0    UDP *:5353
mDNSRespo   21 _mdnsresponder    8u  IPv6 0x3c28a40      0t0    UDP *:5353
configd     27           root    8u  IPv4 0x3c28cc8      0t0    UDP *:*
configd     27           root   12u  IPv6 0x45b8da0      0t0 ICMPV6 *:*
mysqld     103         _mysql   10u  IPv4 0x3f21a68      0t0    TCP *:3306 (LISTEN)
ntpd      3903           root   20u  IPv4 0x3c282a8      0t0    UDP *:123
ntpd      3903           root   21u  IPv6 0x3c28608      0t0    UDP *:123
ntpd      3903           root   22u  IPv6 0x3c28da0      0t0    UDP localhost:123
ntpd      3903           root   23u  IPv4 0x3c28968      0t0    UDP localhost:123
ntpd      3903           root   24u  IPv6 0x3c28890      0t0    UDP localhost:123
ntpd      3903           root   25u  IPv4 0x3c26a30      0t0    UDP 192.168.0.102:123
ntpd      3903           root   27u  IPv6 0x3c27b10      0t0    UDP i-n-d-i-v-e-r.local:123
SystemUIS 4038           indy    9u  IPv4 0x3c287b8      0t0    UDP *:*
GrowlHelp 4047           indy    3u  IPv4 0x4ea3a68      0t0    TCP 192.168.0.102:63110->216.64.142.80:80 (CLOSE_WAIT)
Skype     4048           indy    6u  IPv4 0x3c26370      0t0    UDP localhost:55598
Skype     4048           indy   32u  IPv4 0x3db9e64      0t0    TCP 192.168.0.102:51804->c211-30-0-59.wavrl1.nsw.optusnet.com.au:443 (ESTABLISHED)
Skype     4048           indy   33u  IPv4 0x4281270      0t0    TCP 192.168.0.102:54418->163-158.static.quiettouch.com:80 (CLOSE_WAIT)
Skype     4048           indy   35u  IPv4 0x5f0166c      0t0    TCP *:24242 (LISTEN)
Skype     4048           indy   36u  IPv4 0x3c28530      0t0    UDP *:24242
Skype     4048           indy   37u  IPv4 0x44bb270      0t0    TCP 192.168.5.51:64396->ppp-58-8-80-254.revip2.asianet.co.th:443 (ESTABLISHED)
Python    4446           indy   33u  IPv4 0x3e32270      0t0    TCP *:6881 (LISTEN)
Python    4446           indy   34u  IPv4 0x3c25f38      0t0    UDP *:6881
Python    4446           indy   37u  IPv4 0x569c270      0t0    TCP 192.168.0.102:51831->dial8-20.infoave.net:11289 (ESTABLISHED)
Python    4446           indy   41u  IPv4 0x3db966c      0t0    TCP 192.168.0.102:53414->20129006121.user.veloxzone.com.br:6881 (ESTABLISHED)
Python    4446           indy   44u  IPv4 0x42dde64      0t0    TCP 192.168.0.102:55149->IGLD-83-130-215-156.inter.net.il:42017 (ESTABLISHED)
Python    4446           indy   45u  IPv4 0x5f01a68      0t0    TCP 192.168.0.102:54380->80-254-74-63.static.monzoon.net:29113 (ESTABLISHED)

... more here ...

Using the lsof command (for listing open files), one can find out all the open files that are connecting to the network.

Run it as sudo to give a list of all open files. If you don’t, then you get a list of only those files that are open for the current users.

Coming back to the original reason why I wanted to check the open connections, I could see all the ports that were being used by the torrent client. (I don’t think I’ll do anything to my firewall to “optimize” the connectivity. I’ll just live with the slow download speed!)