Skip to content

Instantly share code, notes, and snippets.

@PetrovStark
Last active August 5, 2022 17:00
Show Gist options
  • Save PetrovStark/451467f867eac03b7caa519e8dff1b0b to your computer and use it in GitHub Desktop.
Save PetrovStark/451467f867eac03b7caa519e8dff1b0b to your computer and use it in GitHub Desktop.
A Python automation for getting access logs massively from a remote server.
#!/usr/bin/env python3
import os
import sys
import getopt
print("""
╔═╗╔═╗╔╦╗ ╦═╗╔═╗╔╦╗╔═╗╔╦╗╔═╗ ╦ ╔═╗╔═╗╔═╗
║ ╦║╣ ║ ╠╦╝║╣ ║║║║ ║ ║ ║╣ ║ ║ ║║ ╦╚═╗
╚═╝╚═╝ ╩ ╩╚═╚═╝╩ ╩╚═╝ ╩ ╚═╝ ╩═╝╚═╝╚═╝╚═╝
""")
print("Developed by: PetrovStark\n\n")
print("It automates the process of getting logs from a remote server.\n\n")
arg_path = ""
arg_remote = ""
arg_id = ""
arg_store = ""
arg_limit = 1
arg_help = "Usage:\n -r <remote> The remote host from which access logs will be catched.\n-i <identity> the path to .pem key.\n -s <store> The path which access logs will be stored\n -l <limit> how many access logs will be accessed from remote server.\n -h <help> Displays this list of commands".format(sys.argv[0])
try:
opts, args = getopt.getopt(sys.argv[1:], "hr:i:s:l:", ["help", "identity=", "remote=", "store=", "limit="])
except Exception as e:
print(e)
print(arg_help)
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print(arg_help) # print the help message
sys.exit(2)
elif opt in ("-r", "--remote"):
arg_remote = arg
elif opt in ("-i", "--identity"):
arg_id = arg
elif opt in ("-s", "--store"):
arg_store = arg
elif opt in ("-l", "--limit"):
arg_limit = int(arg)
os.makedirs(arg_store, exist_ok=True)
actual_log = 0
while arg_limit >= actual_log:
selected_log = f'.{actual_log}.gz' if actual_log > 1 else f'.{actual_log}' if actual_log == 1 else ''
os.system(f'scp -i {arg_id} {arg_remote}:/var/log/apache2/access.log'+ selected_log +f' {arg_store}')
actual_log+=1
os.system(f'gzip -d {arg_store}/*.gz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment