python实现集中式的病毒扫描功能详解

作者:cakincqm 时间:2022-03-04 03:16:00 

本文实例讲述了python实现集中式的病毒扫描功能。分享给大家供大家参考,具体如下:

一 点睛

本次实践实现了一个集中式的病毒扫描管理,可以针对不同业务环境定制扫描策略,比如扫描对象、描述模式、扫描路径、调度频率等。案例实现的架构图如下,首先业务服务器开启clamd服务(监听3310端口),管理服务器启用多线程对指定的服务集群进行扫描,扫描模式、扫描路径会传递到clamd,最后返回扫描结果给管理服务器端。

 python实现集中式的病毒扫描功能详解

本次实战通过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程序设计有所帮助。

来源:https://blog.csdn.net/chengqiuming/article/details/87900489

标签:python,病毒扫描
0
投稿

猜你喜欢

  • asp空间判断jmail组件是否安装或支持的代码

    2011-02-16 10:49:00
  • 如何在Django配置文件里配置session链接

    2022-09-08 18:17:33
  • 超链“确认”对话框confirm

    2008-05-16 11:42:00
  • 用代码帮你了解Python基础(3)

    2021-03-20 07:22:46
  • 如何在浏览器地址栏显示自己的Favicons.ico图标

    2007-10-22 11:45:00
  • python 利用jieba.analyse进行 关键词提取

    2021-03-16 22:11:26
  • numpy.random.choice()函数详解

    2023-04-30 09:46:03
  • 仅允许指定的机器连接SQL Server服务器

    2010-07-22 19:54:00
  • Python的子线程和子进程是如何手动结束的?

    2022-09-08 18:54:41
  • Python通过文本和图片生成词云图

    2023-08-03 09:39:35
  • PHP的RSA加密解密方法以及开发接口使用

    2023-07-13 06:09:20
  • 使用url_helper简化Python中Django框架的url配置教程

    2023-01-10 14:44:51
  • python中pop()函数的语法与实例

    2023-12-26 13:05:31
  • asp 数组 重复删除函数代码

    2011-03-03 10:47:00
  • 如何将多宿主计算机配置为允许SQL Server访问

    2011-01-04 14:04:00
  • 在pandas中遍历DataFrame行的实现方法

    2022-02-16 13:13:26
  • 机器深度学习二分类电影的情感问题

    2022-07-17 06:13:28
  • PDO::setAttribute讲解

    2023-06-05 18:04:23
  • 多级联动下拉选择框,动态获取下一级

    2008-09-04 10:34:00
  • 在OracleE数据库的字段上建立索引的方法

    2009-02-26 10:34:00
  • asp之家 网络编程 m.aspxhome.com