-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweekly-virus-scan.py
More file actions
executable file
·75 lines (64 loc) · 2.88 KB
/
Copy pathweekly-virus-scan.py
File metadata and controls
executable file
·75 lines (64 loc) · 2.88 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
75
#! /bin/bash/env python3
"""
The point of this script is to push out any postive virus scans
and to alert me via pushbullet notifications that would be sent
directly to my mobile device
"""
__author__ = """Reuben deVries"""
__version__ = """0.1"""
__date__ = """Feburary 14th, 2021"""
# importing built-in python modules
import base64
import json
import logging
import os
import subprocess
import sys
# importing 3rd party python modules
import requests
# a function to start the logging process
def start_logging():
# basic logging config - note where the log is stored, the time the event occured and the message.
logging.basicConfig(filename="/var/log/weekly-virus-scan.log",
format="%(asctime)s %(messages)s",
filemode="a")
logger = logging.getLogger() #starting the logging
logger.setLevel(logging.WARN) # setting it to the warn level
return logger #returning the logger so we can pass it to other functions.
# a function to do a daily virus scan on
def daily_virus_scan(logger):
# Adding some variables for the function to work
logger()
results = os.popen("clamscan -r -i --gen-json /").read()
return results
#virus_count = int(result) # transforming the number of viruses found to an int so we can compare it
#if virus_count > 0: # hopefully returns a 0, but if greater then 0 then it's going to return a virus count.
# return virus_count
# a function that will send a notification to my phone via pushbullet api.
def notify_me(logger, results): # inputs the virus count from below.
# Adding some variables for the function to work.
"""calling a OS environment variable that I've stored locally, ideally
I would like to find a better or more secure way to handle this
environment variable."""
logger()
api = os.getenv("API")
api_b64 = base64.b64decode(api)
api_decode = api_b64.decode(encoding="utf-8")
hostname = os.uname()[1] # Lets me know the hostname of the computer where the virus infection was found.
title = "Weekly Clamscan Results on {0}".format(hostname) # The title of the message in the Pushbullet API Post.
data_send = {"type": "note", "title": title, "body": results} # What gets sent to Pushbullet API
resp = requests.post('https://api.pushbullet.com/v2/pushes', data=json.dumps(data_send),
headers={'Authorization': 'Bearer ' + api_decode, 'Content-Type': 'application/json'})
status_code = resp.status_code
if status_code != 200:
raise Exception("Something went wrong")
if __name__ == "__main__":
logger = start_logging
results = daily_virus_scan(logger)
notify_me(logger, results)
#TODO list:
# Find a more secure way to handle the OS environment variable.
# add some proper error handling in the all of the functions
#TODO list:
# Find a more secure way to handle the OS environment variable.
# add some proper error handling in the all of the functions