Apple - How can I list my open network ports with netstat?

netstat -anvp tcp | awk 'NR<3 || /LISTEN/'

Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)     rhiwat shiwat    pid   epid  state    options
tcp46      0      0  *.62981                *.*                    LISTEN      131072 131072  34548      0 0x0100 0x00000006
…

sudo lsof -PiTCP -sTCP:LISTEN

COMMAND     PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
BetterTou 34548 grgarside   20u  IPv4 0xa42a1d0ade5d3585      0t0  TCP *:62981 (LISTEN)
BetterTou 34548 grgarside   21u  IPv6 0xa42a1d0ad67f7a5d      0t0  TCP *:62981 (LISTEN)
…

maybe you can use lsof:

lsof -Pn -i4

-i4 means only show ipv4 address and ports -P and -n fast output

output like this

➜  lsof -Pn -i4 | grep LISTEN
QQPlatfor 22767 xxxx   15u  IPv4 0x36c2bfa04e49385d      0t0  TCP *:49969 (LISTEN)
GoAgentX  33377 xxxx    4u  IPv4 0x36c2bfa06e68b12d      0t0  TCP *:56154 (LISTEN)
GoAgentX  33377 xxxx   20u  IPv4 0x36c2bfa04e492f8d      0t0  TCP 127.0.0.1:56155 (LISTEN)

First, I'm not a BSD expert, but like the OP I wanted the rough equivalent of running the following on a *nix box, or something close:

netstat -tulpn

I read other questions/answers offering lsof* and netstat* on MacOS, and I still wanted something with more compact output. So, this is what I quick put together:

netstat -Watnlv | grep LISTEN | awk '{"ps -o comm= -p " $9 | getline procname;colred="\033[01;31m";colclr="\033[0m"; print cred "proto: " colclr $1 colred " | addr.port: " colclr $4 colred " | pid: " colclr $9 colred " | name: " colclr procname;  }' | column -t -s "|"

It's a bit overkill, so I added color to the output for good measure. Since I'm not going to be able to remember, or want to type, this behemoth. I put it in a bash function and then just call that when needed. Here is said bash function:

macnst (){
    netstat -Watnlv | grep LISTEN | awk '{"ps -o comm= -p " $9 | getline procname;colred="\033[01;31m";colclr="\033[0m"; print colred "proto: " colclr $1 colred " | addr.port: " colclr $4 colred " | pid: " colclr $9 colred " | name: " colclr procname;  }' | column -t -s "|"
}

I have a small collection of these convenience functions in a file that I source from ~/.bash_profile, or ~/.zshrc. This is being added to the collection. It'd be interesting to see other opportunities to make this nicer/slimmer.

Sample Output:

> macns
proto: tcp4     addr.port: 127.0.0.1.9999     pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.35729            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.62087            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.62070            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.62085            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp4     addr.port: *.61993            pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/MacOS/idea
proto: tcp46    addr.port: *.61992            pid: 70065    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.42329    pid: 70065    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.61983    pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.63342    pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.6942     pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.3075     pid: 67931    name:  /Applications/electerm.app/Contents/Frameworks/electerm Helper.app/Contents/MacOS/electerm Helper
proto: tcp6     addr.port: *.58640            pid: 320      name:  /usr/libexec/rapportd
proto: tcp4     addr.port: *.58640            pid: 320      name:  /usr/libexec/rapportd
proto: tcp4     addr.port: 127.0.0.1.9770     pid: 71       name:  /Applications/Pritunl.app/Contents/Resources/pritunl-service