Utility functions

In this section of the tool, I added the utility functions that we will reuse throughout the source code. These functions are shown in the following bullet list:

  • For printing the separators lines, use the following:
separator_single_line = '------------------------------------------------------------'
separator_double_line = '============================================================'
  • For printing the colored texts on the Terminal window, use the following:
# Printing Red Text for errors
def print_red(text): print("33[91m {}33[00m".format (text))


# Printing Green Text for messages
def print_green(text): print("33[92m {}33[00m".format (text))


# Printing Yellow Text for warnings
def print_yellow(text): print("33[93m {}33[00m".format (text))
  • For saving the report results to a file, use the following:
def save_results(results, folder_name, file_name):
try:
# Save the results to a folder/file
file_name_path = folder_name + "/" + file_name

# If the folder does not exist then create it
if not os.path.isdir (folder_name):
os.mkdir (folder_name)

# Create the file object
file_to_save = open (file_name_path, 'w')
# Make sure the output is correctly encoded
results = results.encode ('utf-8')
# Write the changes
file_to_save.write (results)
# Close file object
file_to_save.close ()
except Exception, e:
exception_message = str (e)
print_red ('[!] Error: Cannot save the results to a file! Reason: ' + exception_message)
  • To execute a Terminal window command, use the following:
def execute_cmd(tool_name, cmd):
start_msg = "[+] Starting %s ..." % tool_name
print_green (start_msg)
# The output variable that stores the output from the command line
output = ''

try:
# Cleanup the command string
cmd = cmd.rstrip()
# Execute the command
output += subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
# Add a new line
output += ' '
except Exception, e:
exception_message = str (e)
output += exception_message
print_red ("[!] Error executing the command: " + cmd + " Reason: " + exception_message)
output += ' '

output += separator_single_line + ' '

end_msg = "[+] Finished %s ..." % tool_name
print_green (end_msg)
return output
  • To print error messages after the command's execution, use the following:
def error_execution(tool_name): print_red ("Error Executing " + tool_name)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset