woonizzooni

[Mac] listen 포트 & pid 확인 방법, TCP/UDP 세션 확인 방법 본문

MISC

[Mac] listen 포트 & pid 확인 방법, TCP/UDP 세션 확인 방법

woonizzooni 2019. 7. 20. 00:05

맥 환경에서, 

잘모르는 프레임웍 이용한 서버 동작시,

특정프로그램 이용시,

내가 사용하는 것 외 데이터가 세어나가는지,

내가 모르는 포트를 열어놓은 프로세스가 있는지 (찾아서 kill하려고),

혹은 방화벽 제외 / NAT환경에서의 포트포워딩 정책에 SNAT, DNAT등에 포트 값을 알아내야 할 경우

lsof와 netstast과 다음과 같은 옵션으로 찾으면 된다.

(기타 : linux의 경우 netstat -anp|grep 포트번호 or LISTEN or EST등의 소켓 상태로 필터링해서 사용해왔음)

 


lsof (list open files) 이용시

$ sudo lsof -iTCP -sTCP:LISTEN -n -P 

$ sudo lsof -iTCP -sTCP:LISTEN -n -P
COMMAND    PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
UserEvent   42   root   48u  IPv6 0x8e0ee072f13e4feb      0t0  TCP [fe80:8::aede:48ff:fe00:1122]:49153 (LISTEN)
UserEvent   42   root   49u  IPv6 0x8e0ee072f13e55ab      0t0  TCP [fe80:8::aede:48ff:fe00:1122]:49154 (LISTEN)
UserEvent   42   root   50u  IPv6 0x8e0ee072f13e5b6b      0t0  TCP [fe80:8::aede:48ff:fe00:1122]:49155 (LISTEN)
UserEvent   42   root   51u  IPv6 0x8e0ee072f13e612b      0t0  TCP [fe80:8::aede:48ff:fe00:1122]:49156 (LISTEN)
rapportd   299 xxxxxx    3u  IPv4 0x8e0ee072fca9db2b      0t0  TCP *:49702 (LISTEN)
rapportd   299 xxxxxx    4u  IPv6 0x8e0ee072fd62746b      0t0  TCP *:49702 (LISTEN)
idea      1745 xxxxxx  268u  IPv4 0x8e0ee072fb9a682b      0t0  TCP 127.0.0.1:6942 (LISTEN)
idea      1745 xxxxxx  743u  IPv4 0x8e0ee072fb87c1ab      0t0  TCP 127.0.0.1:63342 (LISTEN)
idea      1745 xxxxxx  794u  IPv4 0x8e0ee072fde094ab      0t0  TCP *:49424 (LISTEN)
idea      1745 xxxxxx  825u  IPv4 0x8e0ee072fde081ab      0t0  TCP 127.0.0.1:49415 (LISTEN)
idea      1745 xxxxxx  842u  IPv4 0x8e0ee072fb9ab1ab      0t0  TCP 127.0.0.1:49727 (LISTEN)
cupsd     3460   root    5u  IPv6 0x8e0ee072fd6296eb      0t0  TCP [::1]:631 (LISTEN)
cupsd     3460   root    6u  IPv4 0x8e0ee072f1401b2b      0t0  TCP 127.0.0.1:631 (LISTEN)
java      3860 xxxxxx  121u  IPv6 0x8e0ee073043ceb6b      0t0  TCP *:49729 (LISTEN)
java      3860 xxxxxx  131u  IPv6 0x8e0ee072fd62a26b      0t0  TCP *:10010 (LISTEN)

 

netstat 이용시 

$ netstat -anv | grep LISTEN 

$ netstat -anv | grep LISTEN
Proto Recv-Q Send-Q  Local Address          Foreign (state) rhiwat shiwat    pid epid  state    options\
                                            Address
tcp46      0      0  *.10010                *.*      LISTEN  131072 131072   3860    0 0x0000 0x00000006
tcp46      0      0  *.49729                *.*      LISTEN  131072 131072   3860    0 0x0100 0x00000006
tcp4       0      0  127.0.0.1.49727        *.*      LISTEN  131072 131072   1745    0 0x0100 0x00000006
tcp6       0      0  *.49702                *.*      LISTEN  131072 131072    299    0 0x0100 0x00000006
tcp4       0      0  *.49702                *.*      LISTEN  131072 131072    299    0 0x0100 0x00000006
tcp4       0      0  *.49424                *.*      LISTEN  131072 131072   1745    0 0x0100 0x00000006
tcp4       0      0  127.0.0.1.49415        *.*      LISTEN  131072 131072   1745    0 0x0100 0x00000006
tcp4       0      0  127.0.0.1.63342        *.*      LISTEN  131072 131072   1745    0 0x0100 0x00000006
tcp4       0      0  127.0.0.1.6942         *.*      LISTEN  131072 131072   1745    0 0x0100 0x00000006
tcp6       0      0  fe80::aede:48ff:.49156 *.*      LISTEN  131072 131072     42    0 0x0180 0x00000006
tcp6       0      0  fe80::aede:48ff:.49155 *.*      LISTEN  131072 131072     42    0 0x0180 0x00000006
tcp6       0      0  fe80::aede:48ff:.49154 *.*      LISTEN  131072 131072     42    0 0x0180 0x00000006
tcp6       0      0  fe80::aede:48ff:.49153 *.*      LISTEN  131072 131072     42    0 0x0180 0x00000006

 

Listen 포트 & 프로세스와 TCP세션 전부 조회

$ sudo lsof -i -n -P | grep TCP
UserEvent   42            root   47u  IPv6 0x8e0ee072f13e4a2b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49152->[fe80:8::aede:48ff:fe33:4455]:59602 (ESTABLISHED)
UserEvent   42            root   48u  IPv6 0x8e0ee072f13e4feb      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49153 (LISTEN)
UserEvent   42            root   49u  IPv6 0x8e0ee072f13e55ab      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49154 (LISTEN)
UserEvent   42            root   50u  IPv6 0x8e0ee072f13e5b6b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49155 (LISTEN)
UserEvent   42            root   51u  IPv6 0x8e0ee072f13e612b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49156 (LISTEN)
logd        60            root   11u  IPv6 0x8e0ee072f13e66eb      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49157->[fe80:8::aede:48ff:fe33:4455]:49169 (ESTABLISHED)
apsd        78            root    7u  IPv4 0x8e0ee072f140082b      0t0    TCP 192.168.0.194:49162->17.57.145.72:5223 (ESTABLISHED)
apsd        78            root    8u  IPv4 0x8e0ee072f140082b      0t0    TCP 192.168.0.194:49162->17.57.145.72:5223 (ESTABLISHED)
usbmuxd     84        _usbmuxd   16u  IPv6 0x8e0ee072fd62a82b      0t0    TCP [fe80:a::df:f28:c2ab:705c]:49719->[fe80:a::1843:44e4:8e9:fd15]:54186 (ESTABLISHED)
bosUpdate  193 _softwareupdate    3u  IPv6 0x8e0ee072fd628b6b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49368->[fe80:8::aede:48ff:fe33:4455]:49159 (ESTABLISHED)
SubmitDia  194            root    3u  IPv6 0x8e0ee072f13e446b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49155->[fe80:8::aede:48ff:fe33:4455]:49172 (ESTABLISHED)
biometric  255            root    4u  IPv6 0x8e0ee072f13e6cab      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49700->[fe80:8::aede:48ff:fe33:4455]:49152 (ESTABLISHED)
Simplenot  284          xxxxxx    5u  IPv4 0x8e0ee072fca9c82b      0t0    TCP 192.168.0.194:49705->192.0.84.247:443 (ESTABLISHED)
Simplenot  284          xxxxxx   15u  IPv4 0x8e0ee072fca9c82b      0t0    TCP 192.168.0.194:49705->192.0.84.247:443 (ESTABLISHED)
rapportd   299          xxxxxx    3u  IPv4 0x8e0ee072fca9db2b      0t0    TCP *:49702 (LISTEN)
rapportd   299          xxxxxx    4u  IPv6 0x8e0ee072fd62746b      0t0    TCP *:49702 (LISTEN)
rapportd   299          xxxxxx   10u  IPv4 0x8e0ee072fb9a71ab      0t0    TCP 192.168.0.194:49702->192.168.0.175:54184 (ESTABLISHED)
Google     394          xxxxxx   23u  IPv4 0x8e0ee072f83181ab      0t0    TCP 192.168.0.194:49698->64.233.189.188:5228 (ESTABLISHED)
Google     394          xxxxxx   24u  IPv4 0x8e0ee072fca9d1ab      0t0    TCP 192.168.0.194:49964->211.231.99.250:443 (CLOSE_WAIT)
findmydev  435            root    4u  IPv6 0x8e0ee072f13e7deb      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49256->[fe80:8::aede:48ff:fe33:4455]:49164 (ESTABLISHED)
corespeec  782          xxxxxx    4u  IPv6 0x8e0ee072f13e726b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49701->[fe80:8::aede:48ff:fe33:4455]:49160 (ESTABLISHED)
mobileact 1278            root    3u  IPv6 0x8e0ee072f13e782b      0t0    TCP [fe80:8::aede:48ff:fe00:1122]:49358->[fe80:8::aede:48ff:fe33:4455]:49162 (ESTABLISHED)
idea      1745          xxxxxx  268u  IPv4 0x8e0ee072fb9a682b      0t0    TCP 127.0.0.1:6942 (LISTEN)
idea      1745          xxxxxx  743u  IPv4 0x8e0ee072fb87c1ab      0t0    TCP 127.0.0.1:63342 (LISTEN)
idea      1745          xxxxxx  794u  IPv4 0x8e0ee072fde094ab      0t0    TCP *:49424 (LISTEN)
idea      1745          xxxxxx  825u  IPv4 0x8e0ee072fde081ab      0t0    TCP 127.0.0.1:49415 (LISTEN)
idea      1745          xxxxxx  842u  IPv4 0x8e0ee072fb9ab1ab      0t0    TCP 127.0.0.1:49727 (LISTEN)
idea      1745          xxxxxx  843u  IPv4 0x8e0ee072fdbfb82b      0t0    TCP 127.0.0.1:49727->127.0.0.1:49728 (ESTABLISHED)
idea      1745          xxxxxx  850u  IPv4 0x8e0ee072fb87de2b      0t0    TCP 127.0.0.1:49415->127.0.0.1:49730 (ESTABLISHED)
java      3859          xxxxxx  115u  IPv4 0x8e0ee072fde08b2b      0t0    TCP 127.0.0.1:49730->127.0.0.1:49415 (ESTABLISHED)
java      3860          xxxxxx  112u  IPv6 0x8e0ee072fd627feb      0t0    TCP 127.0.0.1:49728->127.0.0.1:49727 (ESTABLISHED)
java      3860          xxxxxx  121u  IPv6 0x8e0ee073043ceb6b      0t0    TCP *:49729 (LISTEN)
java      3860          xxxxxx  131u  IPv6 0x8e0ee072fd62a26b      0t0    TCP *:10010 (LISTEN)

 

기타

 

특정 서버 포트(Listen포트) 상태를 보고 싶을 경우

lsof와 netstat을 이용해서 서버포트, 클라이언트포트, pid등 보고 싶은 내용 보면 됨.

$ sudo lsof -i :10010
COMMAND    PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Google     394 xxxxxx   29u  IPv6 0x8e0ee072fd6296eb      0t0  TCP localhost:50245->localhost:10010 (ESTABLISHED)
java      3860 xxxxxx  124u  IPv6 0x8e0ee072fd6285ab      0t0  TCP localhost:10010->localhost:50245 (ESTABLISHED)
java      3860 xxxxxx  131u  IPv6 0x8e0ee072fd62a26b      0t0  TCP *:10010 (LISTEN)

$ netstat -anv|grep '10010'
Proto Recv-Q Send-Q  Local Address  Foreign Address (state)     rhiwat shiwat    pid   epid  state    options
tcp6       0      0  ::1.10010      ::1.50245       ESTABLISHED 407319 146808   3860      0 0x0102 0x00000004
tcp6       0      0  ::1.50245      ::1.10010       ESTABLISHED 407127 146808    394      0 0x0102 0x00000008
tcp46      0      0  *.10010        *.*             LISTEN      131072 131072   3860      0 0x0000 0x00000006

 

pid만 찾을 경우 

lsof -t -i :포트번호

 

IPv4만 (TCP & UDP)

lsof -i 4

 

IPv6만 (TCP & UDP)

lsof -i 6

 

 

[참고]

https://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x

https://blog.jayway.com/2012/09/08/finding-the-pid-listening-on-a-specific-port-on-mac-os-x/

'MISC' 카테고리의 다른 글

Tile World  (0) 2019.07.20
Code Golf 코드 골프, Hello World  (0) 2019.07.20
MSYS2 설치 및 패키지 설치  (0) 2019.07.10
[VSCode] Python 가상 실행 환경 구성 (Windows환경에서)  (0) 2019.06.14
foo bar 기원  (0) 2019.06.13
Comments