Question: Write a script that reads a file periodically ( every 1 0 seconds ) . The file represents the command or commands that your script

Write a script that reads a file periodically (every 10 seconds).
The file represents the command or commands that your script is to process in the background.
Every valid command in the file will begin with simon says:.
The REMAINING part of the line is some text that needs to be appended to another destination file using echo external program.
If the line does not start with simon says, output hackers and a list of current users to a separate file.
Apply the secure coding principles as needed. My answer is below. Let me know if it is correct please. import os
import time
import subprocess
def process_command(command):
if command.startswith("simon says:"):
text_to_append = command[len("simon says:"):].strip()
append_to_file(text_to_append)
else:
report_hackers()
def append_to_file(text):
try:
with open("destination_file.txt","a") as destination_file:
destination_file.write(text +"
")
print("Command processed successfully.")
except Exception as e:
print(f"Error appending to destination file: {e}")
def report_hackers():
try:
users = subprocess.check_output(["who"]).decode("utf-8").strip()
with open("hackers_report.txt","w") as report_file:
report_file.write("hackers
")
report_file.write(users)
print("Hacker report generated.")
except Exception as e:
print(f"Error generating hacker report: {e}")
def main():
while True:
try:
with open("commands.txt","r") as commands_file:
for line in commands_file:
process_command(line.strip())
except Exception as e:
print(f"Error reading commands file: {e}")
time.sleep(10)
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!