温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python怎么实现集中式的病毒扫描功能

发布时间:2021-04-07 09:52:44 来源:亿速云 阅读:153 作者:小新 栏目:开发技术

小编给大家分享一下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怎么实现集中式的病毒扫描功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI