Hello Daniweb,
I'm learning Python at the moment, and I'm starting by looking at automating some of my server tasks, namely things like backups, virus scans and checking for IP changes (being on a dynamic IP, automating this task will be very helpful).
I've started by creating some code which I shall run at night when server load is at it's lowest although being new to Python I am not sure how efficient my code is or how I can improve it. The script runs fine however any advice would be beneficial.
import os
import subprocess
from subprocess import Popen
if os.getuid() != 0 :
raise Exception("\n\nWARNING : YOU MUST RUN THIS APPLICATION AS SU\n\n") # Check if user has sufficient privillages
Log = open('/var/www/ServerOverview.txt', 'a') # Open log file for data entry
print('\n\n-----------------------------------------------------------------')
print('PROCESS STARTING - VIRUS DEFINITION UPDATE AND SCAN')
print('-----------------------------------------------------------------\n\n')
virusDef = Popen(['freshclam']) # Update Virus Definitions
virusDef.wait()
if virusDef.poll() != 0 :
Log.write('\n\n VIRUS DEFINITIONS UPDATE FAILED') # Output if Virus Definitions Update failed
else :
Log.write('\n\n VIRUS DEFINITIONS UPDATE COMPLETE') # Output if Virus Definitions Update was completed
virusScan = Popen(['clamscan', '-r', '/']) # Scan System
virusScan.wait()
if virusScan.poll() != 0 :
Log.write('\n\n VIRUS SCAN FAILED') # Output if Virus Scan failed
else :
Log.write('\n\n VIRUS SCAN COMPLETE') # Output if Virus Scan was completed
Log.close() # Close Log File
Thank you!