OS X - List all active sockets

I'm looking for a simple command that will list ALL currently active and/or otherwise bound TCP & UDP sockets, corresponding port numbers and their respective states (i.e. ESTABLISHED, LISTEN, WAIT, etc.)

Sort of like a reverse nmap scan is what I'm going for here.


From netstat's manual page:

The netstat command symbolically displays the contents of various network-related data structures. There are a number of output formats, depending on the options for the information presented. The first form of the command displays a list of active sockets for each protocol.

pse@Mithos:~$ netstat
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  localhost.8228         localhost.52662        ESTABLISHED
tcp4       0      0  localhost.52662        localhost.8228         ESTABLISHED
tcp4       0      0  192.168.1.30.52661     stackoverflow.co.https ESTABLISHED
tcp4       0      0  localhost.8228         localhost.52656        ESTABLISHED
tcp4       0      0  localhost.52656        localhost.8228         ESTABLISHED
tcp4       0      0  localhost.8228         localhost.52651        ESTABLISHED
...

Another way to do it is to use lsof benefiting it shows the process that owns the sockets. Either combined with grep:

sudo lsof -n -i | grep -e LISTEN -e ESTABLISHED

(just use -e WAIT accordingly if you need it specifically), or with its own filtering based on TCP socket states:

sudo lsof -n -i -s TCP:LISTEN,ESTABLISHED

I know certain versions of netstat show process with the -b switch, but not the OSX/BSD one apparently...