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 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 processcommandcommand:
if command.startswithsimon says:":
texttoappend commandlensimon says:":strip
appendtofiletexttoappend
else:
reporthackers
def appendtofiletext:
try:
with opendestinationfile.txta as destinationfile:
destinationfile.writetext
printCommand processed successfully."
except Exception as e:
printfError appending to destination file: e
def reporthackers:
try:
users subprocess.checkoutputwhodecodeutfstrip
with openhackersreport.txtw as reportfile:
reportfile.writehackers
reportfile.writeusers
printHacker report generated."
except Exception as e:
printfError generating hacker report: e
def main:
while True:
try:
with opencommandstxtr as commandsfile:
for line in commandsfile:
processcommandlinestrip
except Exception as e:
printfError reading commands file: e
time.sleep
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
