最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中如何查看正在使用的端口及其进程ID

Mac crifan 14777浏览 0评论

折腾:

【已解决】pyspider运行出错:Error Could not create web server listening on port 25555

期间,网上都说用:

netstat的-atunlp参数去查看:

<code>netstat -atunlp
</code>

但是此处是Mac,却出错:

<code>➜  AutocarData netstat -atunlp
netstat: option requires an argument -- p
Usage:    netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]
</code>

去看看help说明:

好像Mac中的netstat是简化版本,参数支持的不多?

<code>➜  AutocarData netstat --help
netstat: illegal option -- -
Usage:    netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]

➜  AutocarData which netstat
/usr/sbin/netstat
</code>

然后这个

netstat -atunl

有输出:

<code>➜  AutocarData netstat -atunl
Active LOCAL (UNIX) domain sockets
Address          Type   Recv-Q Send-Q            Inode             Conn             Refs          Nextref Addr
e4d24cdcae041aaf stream      0      0                0 e4d24cdcae042027                0                0 /var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/iTerm2-temp-socket.jhBJgA
e4d24cdcae042027 stream      0      0                0 e4d24cdcae041aaf                0                0
e4d24cdcae04353f stream      0      0                0 e4d24cdcae041857                0                0 /var/run/mDNSResponder
e4d24cdcae041857 stream      0      0                0 e4d24cdcae04353f                0                0
e4d24cdcae042d6f stream      0      0                0 e4d24cdcae0436cf                0                0 /var/run/mDNSResponder
e4d24cdcae0436cf stream      0      0                0 e4d24cdcae042d6f                0                0
e4d24cdcae0413a7 stream      0      0                0 e4d24cdcae041dcf                0                0 /var/run/mDNSResponder
e4d24cdcae041dcf stream      0      0                0 e4d24cdcae0413a7                0                0
e4d24cdcae042ca7 stream      0      0                0 e4d24cdcae04272f                0                0
e4d24cdcae04272f stream      0      0                0 e4d24cdcae042ca7                0                0
e4d24cdcae042e37 stream      0      0                0 e4d24cdcae042fc7                0                0 /var/run/mDNSResponder
e4d24cdcae042fc7 stream      0      0                0 e4d24cdcae042e37                0                0
e4d24cdcae041537 stream      0      0                0 e4d24cdcae0416c7                0                0 /var/run/mDNSResponder
e4d24cdcae0416c7 stream      0      0                0 e4d24cdcae041537                0                0
e4d24cdcaeeed2e7 stream      0      0                0 e4d24cdcaeeeb087                0                0 /var/run/mDNSResponder
...
</code>

但是关键是此处没有搜到25555和phantomjs:

<code>➜  AutocarData netstat -atunl | grep 25555
➜  AutocarData netstat -atunl | grep phantomjs
</code>

看来先要去学习:

Mac中netstat的语法和可用的参数了

netstat 语法 mac

Mac查看端口占用情况 – 破男孩 – 博客园

Mac OS/Linux命令查询网络端口占用情况 – 猫哥_kaiye – 博客园

用-an参数

<code>➜  AutocarData netstat -an | grep 25555
tcp4       0      0  *.25555                *.*                    LISTEN
➜  AutocarData netstat -an | grep phantomjs
</code>

使用 lsof 代替 Mac OS X 中的 netstat 查看占用端口的程序 | TonyDeng’s Blog

而此处Mac中用:

<code>➜  AutocarData lsof -i:25555
COMMAND     PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
phantomjs 46971 crifan   12u  IPv4 0xe4d24cdcaf5e481f      0t0  TCP *:25555 (LISTEN)
</code>

可以达到目的了:

的确看到是phantomjs占用了25555号端口,且PID也看到了。

但是上面竟然netstat中没有看到phantomjs。

还是想要搞清楚Mac中到底能不能用netstat输出对应的,带phantomjs的信息。

所以先去看看netstat本身的语法:

netstat 语法

netstat命令_Linux netstat 命令用法详解:查看Linux中网络系统状态信息

试了半天都不行:

<code>➜  AutocarData netstat -tl | grep phantomjs
➜  AutocarData netstat -a | grep phantomjs
➜  AutocarData netstat -at | grep phantomjs
➜  AutocarData netstat -atl | grep phantomjs
➜  AutocarData netstat -s | grep phantomjs
netstat: sysctl: net.inet.ip.input_perf_data: No such file or directory
➜  AutocarData netstat -st | grep phantomjs
netstat: sysctl: net.inet.ip.input_perf_data: No such file or directory
➜  AutocarData netstat -pat | grep phantomjs
netstat: at: unknown or uninstrumented protocol
➜  AutocarData netstat -p | grep phantomjs
netstat: option requires an argument -- p
Usage:    netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]

➜  AutocarData netstat -pt | grep phantomjs
netstat: t: unknown or uninstrumented protocol
➜  AutocarData netstat -pa | grep phantomjs
netstat: a: unknown or uninstrumented protocol
➜  AutocarData netstat -ap | grep phantomjs
netstat: option requires an argument -- p
Usage:    netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]

➜  AutocarData netstat -an | grep phantomjs
</code>

找到了Mac中的写法不一样:

<code>➜  AutocarData netstat -anp tcp | grep LISTEN
tcp4       0      0  *.25555                *.*                    LISTEN
tcp6       0      0  *.58645                *.*                    LISTEN
tcp4       0      0  *.58645                *.*                    LISTEN
tcp6       0      0  ::1.1089               *.*                    LISTEN
tcp4       0      0  127.0.0.1.1089         *.*                    LISTEN
tcp4       0      0  127.0.0.1.1086         *.*                    LISTEN
tcp4       0      0  127.0.0.1.631          *.*                    LISTEN
tcp6       0      0  ::1.631                *.*                    LISTEN
tcp4       0      0  127.0.0.1.4301         *.*                    LISTEN
tcp4       0      0  127.0.0.1.4300         *.*                    LISTEN
tcp4       0      0  10.108.133.227.5786    *.*                    LISTEN
tcp4       0      0  *.32445                *.*                    LISTEN
tcp4       0      0  127.0.0.1.53939        *.*                    LISTEN
tcp4       0      0  *.49629                *.*                    LISTEN
tcp4       0      0  *.49622                *.*                    LISTEN
tcp4       0      0  *.49619                *.*                    LISTEN
tcp4       0      0  127.0.0.1.63342        *.*                    LISTEN
tcp4       0      0  127.0.0.1.1087         *.*                    LISTEN
tcp4       0      0  127.0.0.1.6942         *.*                    LISTEN
➜  AutocarData netstat -anp udp | grep LISTEN
</code>

再去试试其他:

<code>➜  AutocarData netstat --help
netstat: illegal option -- -
Usage:    netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]

➜  AutocarData netstat -AaLlnW -p tcp
Current listen queue sizes (qlen/incqlen/maxqlen)
Socket           Flowhash Listen         Local Address
e4d24cdcaf5e481f        0 0/0/20         *.25555
e4d24cdc94177437        0 0/0/128        *.58645
e4d24cdcaedfcebf        0 0/0/128        *.58645
e4d24cdc941779f7        0 0/0/16         ::1.1089
e4d24cdcaee3eebf        0 0/0/16         127.0.0.1.1089
e4d24cdca4219ebf        0 0/0/128        127.0.0.1.1086
e4d24cdc96c00ebf        0 0/0/5          127.0.0.1.631
e4d24cdc94178b37        0 0/0/5          ::1.631
e4d24cdca590455f        0 0/0/128        127.0.0.1.4301
e4d24cdc971d3ebf        0 0/0/128        127.0.0.1.4300
e4d24cdc98a0855f        0 0/0/128        10.108.133.227.5786
e4d24cdc9825781f        0 0/0/5          *.32445
e4d24cdca4187ebf        0 0/0/128        127.0.0.1.53939
e4d24cdca5a00ebf        0 0/0/50         *.49629
e4d24cdc976b081f        0 0/0/50         *.49622
e4d24cdca3a5aadf        0 0/0/50         *.49619
e4d24cdca361bebf        0 0/0/128        127.0.0.1.63342
e4d24cdc9f7f455f        0 0/0/128        127.0.0.1.1087
e4d24cdc981c417f        0 0/0/128        127.0.0.1.6942
➜  AutocarData netstat -AaLlnW -p tcp -i
Name  Mtu   Network       Address            Ipkts Ierrs    Opkts Oerrs  Coll
lo0   16384 &lt;Link#1&gt;                        673851     0   673851     0     0
lo0   16384 127           127.0.0.1         673851     -   673851     -     -
                        224.0.0.251
                        224.0.0.1
lo0   16384 ::1/128     ::1                 673851     -   673851     -     -
                        ff02::fb           (refs: 1)
                        ff02::2:ff33:9cc0  (refs: 1)
                        ff01::1            (refs: 1)
                        ff02::1            (refs: 1)
                        ff02::1:ff00:1     (refs: 1)
lo0   16384 fe80::1%lo0 fe80:1::1           673851     -   673851     -     -
                        ff02::fb           (refs: 1)
                        ff02::2:ff33:9cc0  (refs: 1)
                        ff01::1            (refs: 1)
                        ff02::1            (refs: 1)
                        ff02::1:ff00:1     (refs: 1)
gif0* 1280  &lt;Link#2&gt;                             0     0        0     0     0
stf0* 1280  &lt;Link#3&gt;                             0     0        0     0     0
XHC20 0     &lt;Link#4&gt;                             0     0        0     0     0
en0   1500  &lt;Link#5&gt;    ac:bc:32:7f:01:01  1410075     0  1169328     0     0
en0   1500  fe80::4c5:3 fe80:5::4c5:317:e  1410075     -  1169328     -     -
                        ff02::fb           (refs: 1)
                        ff01::1            (refs: 1)
                        ff02::2:ff8e:a3a6  (refs: 1)
                        ff02::1            (refs: 1)
                        ff02::1:ff37:a327  (refs: 1)
en0   1500  192.168.0     192.168.0.103    1410075     -  1169328     -     -
                        224.0.0.251
                        224.0.0.1
p2p0  2304  &lt;Link#6&gt;    0e:bc:32:7f:01:01        0     0        0     0     0
awdl0 1484  &lt;Link#7&gt;    aa:12:b5:cc:d3:db        0     0      101     0     0
awdl0 1484  fe80::a812: fe80:7::a812:b5ff        0     -      101     -     -
                        ff02::fb           (refs: 1)
                        ff01::1            (refs: 1)
                        ff02::2:ff8e:a3a6  (refs: 1)
                        ff02::1            (refs: 1)
                        ff02::1:ffcc:d3db  (refs: 1)
en1   1500  &lt;Link#8&gt;    4a:00:02:71:7c:a0        0     0        0     0     0
en2   1500  &lt;Link#9&gt;    4a:00:02:71:7c:a1        0     0        0     0     0
bridg 1500  &lt;Link#10&gt;   4a:00:02:71:7c:a0        0     0        1     0     0
utun0 2000  &lt;Link#11&gt;                            0     0        2     0     0
utun0 2000  fe80::567f: fe80:b::567f:2dff        0     -        2     -     -
                        ff01::1            (refs: 1)
                        ff02::2:ff14:f234  (refs: 1)
                        ff02::1            (refs: 1)
                        ff02::1:ffd0:1bd3  (refs: 1)
utun1 1380  &lt;Link#12&gt;                            0     0        2     0     0
utun1 1380  fe80::2fc8: fe80:c::2fc8:18ca        0     -        2     -     -
                        ff01::1            (refs: 1)
                        ff02::2:ff14:f234  (refs: 1)
                        ff02::1            (refs: 1)
                        ff02::1:ff4e:3f72  (refs: 1)
➜  AutocarData netstat -AaLlnW -p tcp -r
Routing tables

Internet:
Address  Destination        Gateway            Flags        Refs      Use    Mtu   Netif Expire
default            192.168.0.1        UGSc          165        0   1500     en0
14.17.57.217       192.168.0.1        UGHWIi          5     1522   1500     en0
14.215.140.31      192.168.0.1        UGHW3I          0        6   1500     en0     22
17.252.140.31      192.168.0.1        UGHWIi          2       53   1500     en0
23.37.144.147      192.168.0.1        UGHWIi          1      303   1500     en0
52.80.93.107       192.168.0.1        UGHWIi          2      310   1500     en0
52.206.79.154      192.168.0.1        UGHWIi          1       27   1500     en0
58.87.82.241       192.168.0.1        UGHWIi          1      170   1500     en0
59.37.96.203       192.168.0.1        UGHWIi          1      658   1500     en0
59.111.179.137     192.168.0.1        UGHWIi          1       48   1500     en0
64.233.187.109     192.168.0.1        UGHWIi          2       76   1500     en0
107.23.25.199      192.168.0.1        UGHWIi          1      144   1500     en0
110.75.138.10      192.168.0.1        UGHW3I          0     1603   1500     en0     16
110.75.139.1       192.168.0.1        UGHWIi          3     1519   1500     en0
111.221.29.254     192.168.0.1        UGHW3I          0       39   1500     en0      6
113.96.232.106     192.168.0.1        UGHWIi          4      454   1500     en0
115.159.241.221    192.168.0.1        UGHWIi          2       55   1500     en0
115.159.241.223    192.168.0.1        UGHWIi          2       81   1500     en0
123.151.137.101    192.168.0.1        UGHWIi          2     1435   1500     en0
127                127.0.0.1          UCS             0        0  16384     lo0
127.0.0.1          127.0.0.1          UH             14   520681  16384     lo0
169.254            link#5             UCS             2        0   1500     en0
180.97.8.36        192.168.0.1        UGHWIi          1       14   1500     en0
180.163.25.149     192.168.0.1        UGHWIi          1      162   1500     en0
185.225.12.177     192.168.0.1        UGHWIi          3     6513   1500     en0
192.30.253.124     192.168.0.1        UGHWIi          2      116   1500     en0
192.30.253.125     192.168.0.1        UGHWIi          2      139   1500     en0
192.168.0          link#5             UCS             1        0   1500     en0
192.168.0.1/32     link#5             UCS             1        0   1500     en0
192.168.0.1        2c:30:33:23:9c:d8  UHLWIir        30     1464   1500     en0   1190
192.168.0.102      d4:f4:6f:a:30:80   UHLWIi          3      104   1500     en0    906
192.168.0.103/32   link#5             UCS             0        0   1500     en0
198.252.206.25     192.168.0.1        UGHWIi          2       68   1500     en0
223.252.199.69     192.168.0.1        UGHWIi          1     4918   1500     en0
224.0.0/4          link#5             UmCS            2        0   1500     en0
224.0.0.251        1:0:5e:0:0:fb      UHmLWI          0        0   1500     en0
239.255.255.250    1:0:5e:7f:ff:fa    UHmLWI          0      176   1500     en0
255.255.255.255/32 link#5             UCS             0        0   1500     en0

Internet6:
Address  Destination                             Gateway                         Flags        Refs      Use    Mtu    Netif Expire
default                                 fe80::%utun0                    UGcI            0        0   2000    utun0
default                                 fe80::%utun1                    UGcI            0        0   1380    utun1
::1                                     ::1                             UHL             1   153574  16384      lo0
fe80::%lo0/64                           fe80::1%lo0                     UcI             1        0  16384      lo0
fe80::1%lo0                             link#1                          UHLI            0        0  16384      lo0
fe80::%en0/64                           link#5                          UCI             1        0   1500      en0
fe80::4c5:317:e637:a327%en0             ac:bc:32:7f:1:1                 UHLI            1        0  16384      lo0
fe80::%awdl0/64                         link#7                          UCI             1        0   1484    awdl0
fe80::a812:b5ff:fecc:d3db%awdl0         aa:12:b5:cc:d3:db               UHLI            1        0  16384      lo0
fe80::%utun0/64                         fe80::567f:2dff:b0d0:1bd3%utun0 UcI             2        0   2000    utun0
fe80::567f:2dff:b0d0:1bd3%utun0         link#11                         UHLI            0        0  16384      lo0
fe80::%utun1/64                         fe80::2fc8:18ca:f04e:3f72%utun1 UcI             2        0   1380    utun1
fe80::2fc8:18ca:f04e:3f72%utun1         link#12                         UHLI            0        0  16384      lo0
ff01::%lo0/32                           ::1                             UmCI            0        0  16384      lo0
ff01::%en0/32                           link#5                          UmCI            0        0   1500      en0
ff01::%awdl0/32                         link#7                          UmCI            0        0   1484    awdl0
ff01::%utun0/32                         fe80::567f:2dff:b0d0:1bd3%utun0 UmCI            0        0   2000    utun0
ff01::%utun1/32                         fe80::2fc8:18ca:f04e:3f72%utun1 UmCI            0        0   1380    utun1
ff02::%lo0/32                           ::1                             UmCI            0        0  16384      lo0
ff02::%en0/32                           link#5                          UmCI            0        0   1500      en0
ff02::%awdl0/32                         link#7                          UmCI            0        0   1484    awdl0
ff02::%utun0/32                         fe80::567f:2dff:b0d0:1bd3%utun0 UmCI            0        0   2000    utun0
ff02::%utun1/32                         fe80::2fc8:18ca:f04e:3f72%utun1 UmCI            0        0   1380    utun1
➜  AutocarData netstat --help
netstat: illegal option -- -
Usage:    netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]

➜  AutocarData netstat -AaLlnW -p tcp -rs
routing:
    0 bad routing redirect
    0 dynamically created route
    0 new gateway due to redirects
    4294956769 destination found unreachable
    0 use of a wildcard route
    0 lookup returned indirect routes pointing to indirect gateway route
    3 routes not in table but not freed
</code>

但是还是没看到有:

phantomjs

Mac netstat show pid

macos – Who is listening on a given TCP port on Mac OS X? – Stack Overflow

都是说用lsof

<code>➜  AutocarData netstat -anv | grep 25555
tcp4       0      0  *.25555                *.*                    LISTEN      131072 131072  46971      0
➜  AutocarData netstat -anv | grep phantomjs
</code>

Finding the pid listening on a specific port on Mac OS X – Jayway

networking – How to use netstat to show what process is listening on a port – Super User

参考:

Mac OS X: List listening ports and programs using netstat

解释的比较清楚:

Linux中的:

<code>netstat -pant | grep LISTEN
</code>

而mac中,是不识别的:

<code>➜  ~ netstat -pant | grep LISTEN
netstat: ant: unknown or uninstrumented protocol
</code>

需要用:

<code>netstat -an -ptcp | grep LISTEN
</code>

可以看到端口,但是是看不到program的,因为此处的-p参数只是protocl,不是program:

<code>➜  ~ netstat -an -ptcp | grep LISTEN
tcp4       0      0  *.5000                 *.*                    LISTEN
tcp4       0      0  127.0.0.1.23333        *.*                    LISTEN
tcp4       0      0  *.25555                *.*                    LISTEN
tcp6       0      0  *.58629                *.*                    LISTEN
tcp4       0      0  *.58629                *.*                    LISTEN
tcp6       0      0  ::1.1089               *.*                    LISTEN
tcp4       0      0  127.0.0.1.1089         *.*                    LISTEN
tcp4       0      0  127.0.0.1.1086         *.*                    LISTEN
tcp4       0      0  127.0.0.1.631          *.*                    LISTEN
tcp6       0      0  ::1.631                *.*                    LISTEN
tcp4       0      0  127.0.0.1.4301         *.*                    LISTEN
tcp4       0      0  127.0.0.1.4300         *.*                    LISTEN
tcp4       0      0  10.108.133.227.5786    *.*                    LISTEN
tcp4       0      0  *.32445                *.*                    LISTEN
tcp4       0      0  127.0.0.1.53939        *.*                    LISTEN
tcp4       0      0  *.49629                *.*                    LISTEN
tcp4       0      0  *.49622                *.*                    LISTEN
tcp4       0      0  *.49619                *.*                    LISTEN
tcp4       0      0  127.0.0.1.63342        *.*                    LISTEN
tcp4       0      0  127.0.0.1.1087         *.*                    LISTEN
tcp4       0      0  127.0.0.1.6942         *.*                    LISTEN
</code>

然后去看lsof的help:

<code>➜  ~ lsof --help
lsof: illegal option character: -
lsof: illegal option character: e
lsof: no process ID specified
lsof 4.89
 latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
 latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
 latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
 usage: [-?abhlnNoOPRtUvV] [+|-c c] [+|-d s] [+D D] [+|-f[cgG]]
 [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+|-M] [-o [o]] [-p s]
 [+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
  -?|-h list help          -a AND selections (OR)     -b avoid kernel blocks
  -c c  cmd c ^c /c/[bix]  +c w  COMMAND width (9)    +d s  dir s files
  -d s  select by FD set   +D D  dir D tree *SLOW?*   -i select IPv[46] files
  -l list UID numbers      -n no host names           -N select NFS files
  -o list file offset      -O no overhead *RISKY*     -P no port names
  -R list paRent PID       -s list file size          -t terse listing
  -T disable TCP/TPI info  -U select Unix socket      -v list version info
  -V verbose search        +|-w  Warnings (+)         -- end option scan
  +f|-f  +filesystem or -file names     +|-f[cgG] Ct flaGs
  -F [f] select fields; -F? for help
  +|-L [l] list (+) suppress (-) link counts &lt; l (0 = all; default = 0)
  +|-M   portMap registration (-)       -o o   o 0t offset digits (8)
  -p s   exclude(^)|select PIDs         -S [t] t second stat timeout (15)
  -T fqs TCP/TPI Fl,Q,St (s) info
  -g [s] exclude(^)|select and print process group IDs
  -i i   select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
  +|-r [t[m&lt;fmt&gt;]] repeat every t seconds (15);  + until no files, - forever.
       An optional suffix to t is m&lt;fmt&gt;; m must separate t from &lt;fmt&gt; and
      &lt;fmt&gt; is an strftime(3) format for the marker line.
  -s p:s  exclude(^)|select protocol (p = TCP|UDP) states by name(s).
  -u s   exclude(^)|select login|UID set s
  -x [fl] cross over +d|+D File systems or symbolic Links
  names  select named files or files on named file systems
Anyone can list all files; /dev warnings disabled; kernel ID check disabled.
</code>

【总结】

netstat本身的命令,是能找到某个端口的pip的:

比如之前用过的

<code>netstat -lanp | grep 25555
</code>

但是Mac中大netstat是精简版,弱智版本,不支持。

最多能用:

<code>➜  AutocarData netstat -anv | grep 25555
tcp4       0      0  *.25555                *.*                    LISTEN      131072 131072  46971      0
</code>

看到端口,但是不知道是谁占用的。更不知道其pid。

mac中可以用lsof:

<code>➜  AutocarData lsof -i:25555
COMMAND     PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
phantomjs 46971 crifan   12u  IPv4 0xe4d24cdcaf5e481f      0t0  TCP *:25555 (LISTEN)
</code>

即可看出:

是phantomjs占用了2555端口。

pid是46971

-》后续就可以用kill去杀掉这个应用了。

转载请注明:在路上 » 【已解决】Mac中如何查看正在使用的端口及其进程ID

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.136 seconds, using 22.14MB memory