本文實例講述了python實現集中式的病毒掃描功能。分享給大家供大家參考,具體如下:
一?點睛
本次實踐實現了一個集中式的病毒掃描管理,可以針對不同業務環境定制掃描策略,比如掃描對象、描述模式、掃描路徑、調度頻率等。案例實現的架構圖如下,首先業務服務器開啟clamd服務(監聽3310端口),管理服務器啟用多線程對指定的服務集群進行掃描,掃描模式、掃描路徑會傳遞到clamd,最后返回掃描結果給管理服務器端。
本次實戰通過ClamdNetworkSocket()方法實現與業務服務器建立掃描socket連接,再通過啟動不同掃描方式實施病毒掃描并返回結果。
二?代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import pyclamd
from threading import Thread
class Scan(Thread):
def __init__ (self,IP,scan_type,file):
"""構造方法"""
Thread.__init__(self)
self.IP = IP
self.scan_type=scan_type
self.file = file
self.connstr=""
self.scanresult=""
def run(self):
"""多進程run方法"""
try:
cd = pyclamd.ClamdNetworkSocket(self.IP,3310)
if cd.ping():
self.connstr=self.IP+" connection [OK]"
cd.reload()
if self.scan_type=="contscan_file":
self.scanresult="{0}\n".format(cd.contscan_file(self.file))
elif self.scan_type=="multiscan_file":
self.scanresult="{0}\n".format(cd.multiscan_file(self.file))
elif self.scan_type=="scan_file":
self.scanresult="{0}\n".format(cd.scan_file(self.file))
time.sleep(1)
else:
self.connstr=self.IP+" ping error,exit"
return
except Exception,e:
self.connstr=self.IP+" "+str(e)
IPs=['192.168.0.120']
scantype="multiscan_file"
scanfile="/data"
i=1
threadnum=2
scanlist = []
for ip in IPs:
currp = Scan(ip,scantype,scanfile)
scanlist.append(currp)
if i%threadnum==0 or i==len(IPs):
for task in scanlist:
task.start()
for task in scanlist:
task.join()
print task.connstr
print task.scanresult
scanlist = []
i+=1
三?結果
1?無病毒的情況下,掃描結果
E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/4_1_2.py
192.168.0.120 connection [OK]
None
2?有病毒的情況下,掃描結果
2.1?制作病毒測試文件
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
2.2?掃描結果
E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/4_1_2.py
192.168.0.120 connection [OK]
{u'/data/EICAR': ('FOUND', 'Eicar-Test-Signature')}
更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python進程與線程操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

