本文實(shí)例講述了python實(shí)現(xiàn)集中式的病毒掃描功能。分享給大家供大家參考,具體如下:
一?點(diǎn)睛
本次實(shí)踐實(shí)現(xiàn)了一個(gè)集中式的病毒掃描管理,可以針對(duì)不同業(yè)務(wù)環(huán)境定制掃描策略,比如掃描對(duì)象、描述模式、掃描路徑、調(diào)度頻率等。案例實(shí)現(xiàn)的架構(gòu)圖如下,首先業(yè)務(wù)服務(wù)器開(kāi)啟clamd服務(wù)(監(jiān)聽(tīng)3310端口),管理服務(wù)器啟用多線程對(duì)指定的服務(wù)集群進(jìn)行掃描,掃描模式、掃描路徑會(huì)傳遞到clamd,最后返回掃描結(jié)果給管理服務(wù)器端。
本次實(shí)戰(zhàn)通過(guò)ClamdNetworkSocket()方法實(shí)現(xiàn)與業(yè)務(wù)服務(wù)器建立掃描socket連接,再通過(guò)啟動(dòng)不同掃描方式實(shí)施病毒掃描并返回結(jié)果。
二?代碼
#!/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): """構(gòu)造方法""" Thread.__init__(self) self.IP = IP self.scan_type=scan_type self.file = file self.connstr="" self.scanresult="" def run(self): """多進(jìn)程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
三?結(jié)果
1?無(wú)病毒的情況下,掃描結(jié)果
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?有病毒的情況下,掃描結(jié)果
2.1?制作病毒測(cè)試文件
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
2.2?掃描結(jié)果
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')}
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
