Jan-02-2019, 01:19 AM
I am trying to modify the lines in a file. After the first few lines in a rules file, there are a lot of commented out rules, and it follows this format:
# alert tcp...
After running the script, I notice that the new file output is getting cut off. I checked the original file, and all of the content was there. So it seems somewhere my script is cutting text off at the end of the rules file. I've provided the function below that modifies such a rules file.
# alert tcp...
After running the script, I notice that the new file output is getting cut off. I checked the original file, and all of the content was there. So it seems somewhere my script is cutting text off at the end of the rules file. I've provided the function below that modifies such a rules file.
def updateFile (status, snortRulesFileName): print "working with file " + snortRulesFileName ruleFile = open(snortRulesFileName, 'r') w = open('output.txt', 'w') if status.lower() == "enable": for line in ruleFile: # Get the first 7 characters chars = line[:7] if chars == "# alert": line = line[2:] # Remove first two beginning characters # Write the rule to another file w.write(line) if status.lower() == "disable": for line in ruleFile: # Add the comment chars # Get the first five characters of the line chars = line[:5] if chars == "alert": line = '# ' + line # print line w.write(line) # Copy the output file to the original file copyfile('output.txt', snortRulesFileName) # Then delete the output file os.remove('output.txt') w.close()