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