파이썬을 이용한 윈도우의 netstat -n 명령어를 이용하여 아웃바운드IP와 포트 그리고 빈도수를 출력
* netstat : 모든 연결과 수신 대기 포트를 표시
* option : -n 주소와 포트 번호를 숫자 형식으로 표시
netstat -n 명령어의 출력을 지정된 파일에 적재. (sample 초당 100번. 많으면 많을 수록 더 많은 데이터가 수집)
import os
import time
cnt = 0
while cnt < 100:
os.system('netstat -n >> 경로') # netstat -n 지정된 경로 파일적재
cnt += 1
time.sleep(1)
자신의 IP와 127.0.0.1을 제외한 IP와 포트 그리고 빈도 수를 출력
import collections
import re
ipnPort = re.findall(r'\d+[.]\d+[.]\d+[.]\d+[:]\d+', open(filename).read().lower())
for x, y in collections.Counter(ipnPort).most_common():
if str(x).rpartition(':')[0] == '127.0.0.1' or str(x).rpartition(':')[0] == '자신IP':
pass
else:
print(str(x)+'\t'+str(y))
'Python > Python for Windows' 카테고리의 다른 글
[Python] Check shared folder of Windows operating system using subprocess and regular expression (0) | 2018.05.04 |
---|---|
[Python] SQLite 메모리DB를 활용한 파이썬 IIS 로그파서 (0) | 2018.04.27 |
[Python] xcopy DOS명령어를 조합한 증분 백업 (0) | 2018.04.20 |
[Python] 윈도우 서버에서 SMTP서버로 메일 발송 (2) (0) | 2018.04.18 |
[Python] 자신의 로컬 IP 알아내기 (0) | 2018.04.18 |
zipfile을 이용한 특정폴더에 있는 모든 파일 압축 (0) | 2018.04.18 |
[Python] Counters를 이용한 IIS 웹 로그 IP주소 빈도 수 추출 (0) | 2018.04.13 |
[Python] 하위 디렉토리 파일크기와 라인 수 추출 (0) | 2018.04.12 |