-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclamscan_multi.py
More file actions
75 lines (63 loc) · 2.51 KB
/
clamscan_multi.py
File metadata and controls
75 lines (63 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3
import subprocess
import multiprocessing as mp
import time
import logging
import os
import sys
def run_clamscan():
# Configure basic logging (e.g., to a file or console)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s',
handlers = [
logging.FileHandler("clamav.log", mode="a"),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
try:
scan_file="/media/music.flac"
print(f"Start ClamAV scan with [PID: {os.getpid()}]")
scan_dir = ["/usr/bin/clamscan", "-rv", "--move=/root/clam_infected",scan_file]
result = subprocess.run(
scan_dir,
capture_output = True, # Captures stdout and stderr
text = True, # Decodes output as a string (instead of bytes)
check = True, # Raise CalledProcessError if the command fails
encoding='utf-8' # Specify encoding if needed
)
if result.stdout:
logger.info("STDOUT:\n%s", result.stdout.strip())
if result.stderr:
logger.warning("STDERR:\n%s", result.stderr.strip())
except subprocess.CalledProcessError as e:
logger.error("Command Failed with return code %s", e.returncode)
if e.stdout:
logger.error("STDOUT on error:\n%s", e.stdout.strip())
if e.stderr:
logger.error("STDERR on error:\n%s", e.stdout.strip())
time.sleep(3)
print("Task clamscan finished")
def read_log():
output = subprocess.run("tail -f /var/log/clamav.log", shell=True)
print(f"reading clam scan log, (PID: {os.getpid()})")
# Simulate ongoing work
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break
print("Reading process stopped")
if __name__ == "__main__":
start_time = time.time() # Use perf_counter for better precision
p1 = mp.Process(target=run_clamscan)
p2 = mp.Process(target=read_log)
p1.start()
p2.start()
p1.join()
# p1 finished, now terminate p2
if p2.is_alive():
print("ClamAV scan finished, terminate reading log task")
p2.terminate()
# p2.join() # Ensure the process is fully terminated and clean up
end_time = time.time()
print(f"\nWhole execution time: {end_time - start_time:.2f} seconds")