Python/Python for Windows 37

[Python] subprocess를 이용한 윈도우 방화벽 ON/OFF 점검

시스템 관리를 하다 윈도우 방화벽 상태를 주기적으로 체크해야 할 일이 있어서 간단히 파이썬과 명령어를 조합하여 만들어 보았다. subprocess.check_output : 명령어 결과를 문자열로 리턴 받음 import subprocess if 'ON' in str(subprocess.check_output(['netsh', 'advfirewall', 'show', 'currentprofile', 'state'], universal_newlines=True)): print('Windows Firewall State ON') else: print('Windows Firewall State OFF') RESULT : Windows Firewall State ON RESULT : Windows Firewall..

[Python] subprocess모듈과 7z을 이용한 전일 로그 자동 압축 보관

7z은 커맨드 명령어를 제공하며, 오픈소스이다. 다운로드 페이지 : https://www.7-zip.org/download.html Download Download .7z Any / x86 / x64 LZMA SDK: (C, C++, C#, Java) www.7-zip.org 이번 포스팅에서 쓰는 옵션은 압축(a) 옵션을 사용하여 전일 로그를 압축해보려 한다. 다양한 옵션을 이용하려면 포스팅 하단의 URL 경로나 7z.exe 명령어를 실행하면 알 수 있다. 대상 로그는 IIS로그로 포맷은 ex_yyyymmdd.log 형식이며, 현재 2019-10-24일 기준으로 전일 기준인 ex_20191023.log 파일이다. import subprocess from datetime import timedelta fr..

[Python] subprocess모듈과 Bandizip을 이용한 전일 로그 자동 압축 보관

Bandizip은 커맨드 명령어를 제공하며, 자세한 옵션은 아래 URL을 참고 URL https://www.bandisoft.com/bandizip/help/parameter/ 이번 포스팅에서 쓰는 옵션은 압축(c)과 자동 창 닫힘(-y)을 사용하여 전일 로그를 압축해보려 한다. 대상 로그는 IIS로그로 포맷은 ex_yyyymmdd.log 형식이며, 현재 2019-10-20일 기준으로 전일 기준인 ex_20191019.log 파일이다. import subprocess from datetime import timedelta from datetime import datetime from os import chdir yesterdaylog = 'ex_'+str(datetime.today().date() - t..

[Python] To send an attachment when mailing

메일발송시 첨부파일을 포함하여 발송하기 첨부파일 : test.mp4 import smtplib from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email import encoders msg = MIMEMultipart() msg['Subject'] = ' ' msg['From'] = '' msg['To'] = '' with open('test.mp4', 'rb') as f: part = MIMEBase('application', 'octet-stream') part.set_payload(f.read()) encoders.encode_base64(part) part.add_header('Conte..

[Python] psutil을 이용한 프로세스/로컬IP/로컬Port/리모트IP/리모트Port 모니터링

psutil은 실행중인 프로세스 및 시스템 자원 정보를 볼 수 있는 모듈 import psutil strings = '' stringsFormat = '%-30s\t%-20s\t%-10s\t%-20s\t%-10s\n' strings = stringsFormat % ('process', 'local ip', 'local port', 'remote ip', 'remote port') strings += '-'*30+'\t'+'-'*20+'\t'+'-'*10+'\t'+'-'*20+'\t'+'-'*10+'\n' def processinfo(): process = psutil.Process(conn.pid) return process.name() for conn in psutil.net_connections():..

[Python] Check ICMP using ping3 module, maria db and grafana

이번 포스팅은 1부에서 명령어기반으로 구현한 ICMP 체크를 grafana 오픈소스를 이용해 시각화 모니터링을 구현해보겠다. - 모듈 : ping3 - 적재DB : mariadb mariadb DB 테이블 만들기 - logtime : ICMP를 체크한 현재 시간 → (str(datetime.now())[:19],site[0]) - host : ICMP 체크 대상 IP 주소 → (Site[0]) - pingstatus : 정상(0), 실패(1) → (ping status 정상 : 0, ping fail : 1) import time from datetime import datetime from ping3 import ping,verbose_ping import pymysql conn = pymysql.co..

[Python] winreg 모듈을 이용한 Windows 공유폴더 레지스트리 설정 점검

파이썬 winreg 모듈을 이용한 PC 공유폴더 레지스트리 설정 점검 - PC : AutoShareWks - Server : AutoShareServer DOC : https://docs.python.org/3/library/winreg.html winreg — Windows registry access — Python 3.7.4 documentation winreg — Windows registry access These functions expose the Windows registry API to Python. Instead of using an integer as the registry handle, a handle object is used to ensure that the handles are..

[Python] check ICMP using ping3 module. (파이썬을 이용한 ping 체크)

시스템을 운영하다 보면 기본적으로 서버나 네트워크 장비 ICMP 핼스 체크를 지속적으로 해야하는데, python의 ping3 모듈을 이용하여 구현할 수 있다. 모듈 설치 : pip install ping3 소스설명 ​ 우선 프로그램이 pinglist.txt 파일에 ping check 해야하는 리스트를 기입한다. ​ 테스트IP를 설명하자면 203 IP서버는 네트워크가 단절되어 있고, #으로 시작하는 IP는 주석으로 인식하여 check 대상에서 제외된다. ​ IP 서버설명 10.x.x.201 Test_Server1 10.x.x.202 Test_Server2 10.x.x.203 Test_Server3 → 네트워크 단절 10.x.x.204 Test_Server5 10.x.x.205 Test_Server6 #10..