Answers:
netstat -ap tcp | grep -i "listen"
Achive Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 localhost.25035 *.* LISTEN
sudo lsof -PiTCP -sTCP:LISTEN
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
GitHub 850 grgarside 6u IPv4 0x23c345381d089301 0t0 TCP localhost:25035 (LISTEN)
sudo lsof ...
。没有sudo,lsof只能看到您拥有的进程,因此不会显示系统进程打开的任何端口。另外,您可以通过告诉lsof您只想查看LISTEN状态的端口,从而跳过grep命令sudo lsof -PiTCP -sTCP:LISTEN
。
grep -i "listen"
您的2个示例中的影响。
netstat
,您可以选择列出打开每个端口的进程的pid。还有什么办法可以使BSD获得这种行为netstat
吗?
-v
标志打开了它。pid将位于倒数第二列。
也许您可以使用lsof:
lsof -Pn -i4
-i4表示仅显示ipv4地址和端口-P和-n快速输出
这样的输出
➜ 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)
lsof
列出打开的文件。网络套接字算作文件,因此每个打开的网络套接字(正在侦听或正在使用中)将在中列出lsof
。
lsof -Pn -i6
for IPV6
-i4
仅查看IPv4地址的切换。
最简单的方法是使用netstat
:
$ netstat -ap tcp
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 10.0.2.23.58792 17.172.233.109.5223 ESTABLISHED
tcp4 87 0 my_iMac__at_home.55481 stackoverflow.co.http ESTABLISHED
tcp4 116 0 my_iMac__at_home.55478 stackoverflow.co.http ESTABLISHED
tcp4 58 0 my_iMac__at_home.63452 stackoverflow.co.http ESTABLISHED
tcp4 87 0 my_iMac__at_home.63429 stackoverflow.co.http ESTABLISHED
tcp4 0 0 localhost.63173 localhost.773 ESTABLISHED
tcp4 0 0 localhost.773 localhost.63173 ESTABLISHED
tcp4 0 0 localhost.63173 *.* LISTEN
tcp4 0 0 localhost.63172 *.* LISTEN
tcp4 0 0 localhost.ipp *.* LISTEN
tcp6 0 0 localhost.ipp *.* LISTEN
…不添加任何过滤,以获取正确的标头,并查看两个服务器在侦听,并且已经在两个方向建立了连接。在此示例中,第一行显示了从我的Mac到的连接
17.172.233.109
,进一步表明:
whois 17.172.233.109
告诉我它位于Apple。
首先,我不是BSD专家,但是像OP一样,我希望大致相当于在* nix框或类似的东西上运行以下命令:
netstat -tulpn
我阅读了在MacOS上提供lsof *和netstat *的其他问题/答案,但我仍然想要一些输出更紧凑的东西。因此,这就是我快速总结的内容:
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 "|"
这有点矫kill过正,所以我在输出中添加了颜色以达到很好的效果。由于我将无法记住或想要输入此庞然大物。我将其放在bash函数中,然后在需要时调用它。这里说bash函数:
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 "|"
}
我在〜/ .bash_profile或〜/ .zshrc的文件中收集了这些便利功能的一小部分。这将被添加到集合中。看到使这个更好/更微妙的其他机会很有趣。
样本输出:
> 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
lsof
要好一些,谢谢。