-
Python - 프로세스 네트워크 연결상태 확인방법(tcpview)Programming/Python 2017. 8. 14. 23:15반응형
tcpview처럼 프로세스별 네트워크 연결상태를 확인하는 방법을 알아보니 psutil 을 사용하여 확인이 가능하다고 하네요
psutil를 설치하고 조금 비슷하게 따라해봤습니다.
''' 네트워크 연결상태 조회 ''' import psutil def tcpview(): strOut = '' strFormat = '%-30s\t%-5s\t%-15s\t%-20s\t%-5s\t%-20s\t%-5s\n' strOut = strFormat% ('process','pid','status','local address','port','remote address','port') strOut += '------------------------------\t-----\t---------------\t--------------------\t-----\t--------------------\t-----\n' for conn in psutil.net_connections(): if conn.status == 'NONE': continue proc = psutil.Process(conn.pid) name = proc.name() pid = conn.pid status = conn.status local_ip = conn.laddr[0] local_port = conn.laddr[1] remote_ip = conn.raddr[0] if conn.raddr else '' remote_port = conn.raddr[1] if conn.raddr else '' strOut += strFormat%(name, pid, status, local_ip, local_port, remote_ip, remote_port) print(strOut) tcpview() ''' 실행결과 c:\Projects\JKSample\Python>tcpview.py process pid status local address port remote address port ------------------------------ ----- --------------- -------------------- ----- -------------------- ----- services.exe 696 LISTEN :: 1544 chrome.exe 10356 ESTABLISHED 192.168.0.8 2185 38.127.167.14 443 TeamViewer_Service.exe 2924 ESTABLISHED 127.0.0.1 5939 127.0.0.1 1592 AnySign4PC.exe 5536 LISTEN 127.0.0.1 10530 TeamViewer.exe 5284 ESTABLISHED 127.0.0.1 1596 127.0.0.1 1597 System 4 LISTEN :: 445 chrome.exe 10356 ESTABLISHED 192.168.0.8 3796 172.217.24.194 80 svchost.exe 1020 LISTEN :: 7680 nosstarter.npe 7004 LISTEN 0.0.0.0 14440 wininit.exe 548 LISTEN :: 1536 mDNSResponder.exe 2708 LISTEN 127.0.0.1 5354 AnySign4PCLauncher.exe 2632 LISTEN 127.0.0.1 31026 chrome.exe 10356 ESTABLISHED 192.168.0.8 3795 172.217.24.194 80 nkrunlite.exe 12988 CLOSE_WAIT 192.168.0.8 1760 58.229.136.151 443 APSDaemon.exe 9332 ESTABLISHED 192.168.0.8 1640 17.252.140.104 5223 wininit.exe 548 LISTEN 0.0.0.0 1536 jhi_service.exe 3752 LISTEN ::1 1561 iCloudServices.exe 8924 CLOSE_WAIT 192.168.0.8 1628 17.248.157.75 443 svchost.exe 3408 LISTEN 0.0.0.0 22 ASDSvc.exe 2808 CLOSE_WAIT 192.168.0.8 3777 211.115.106.201 80 chrome.exe 10356 ESTABLISHED 192.168.0.8 3797 104.16.94.65 443 SKCertService.exe 9948 LISTEN 127.0.0.1 14098 nkrunlite.exe 12988 CLOSE_WAIT 192.168.0.8 1761 58.229.136.151 443 AppleMobileDeviceService.exe 2616 ESTABLISHED 127.0.0.1 1541 127.0.0.1 5354 picpick.exe 8468 CLOSE_WAIT 192.168.0.8 1619 192.99.63.220 80 TeamViewer_Service.exe 2924 ESTABLISHED 192.168.0.8 1546 37.252.224.2 5938 lsass.exe 704 LISTEN 0.0.0.0 1549 StSess.exe 8300 LISTEN 0.0.0.0 55920 NSpeedMeter.exe 2780 LISTEN 0.0.0.0 12345 '''
전체 네트워크 정보를 가져올때는
psutil.net_connections()
특정 프로세스의 네트워크 정보를 가져올때는
p = psutil.Process(프로세스id) p.connections()
.
반응형'Programming > Python' 카테고리의 다른 글
pip SSL: CERTIFICATE_VERIFY_FAILED 오류 (2) 2022.05.26 Scrapy 윈도우 설치시 utf-8 관련 오류 해결방법 (0) 2018.02.28 파이썬 - 프로세스 관리자권한 여부 확인 (2) 2017.08.13