Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions