Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Created June 23, 2022 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanleoncz/1dd25796f36c811c821d52a97d02b9c4 to your computer and use it in GitHub Desktop.
Save ivanleoncz/1dd25796f36c811c821d52a97d02b9c4 to your computer and use it in GitHub Desktop.
Create FAKE Nginx Access Log File (for simulation, testing and exercising purposes)
from datetime import datetime, timedelta
from ipaddress import ip_network
import random
filename = 'fake_access.log'
number_of_lines = 20
ips = list(ip_network('123.25.44.0/28').hosts())
ips += list(ip_network('25.1.4.128/28').hosts())
ips += list(ip_network('13.250.24.0/28').hosts())
ips += list(ip_network('78.21.94.128/28').hosts())
endpoints = ['/api/tasks/', '/api/owners/', '/api/groups/']
uas = ['curl/7.68.0',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:101.0) Gecko/20100101 Firefox/101.0',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36']
http_codes = ['200', '200', '200', '301', '404']
dates = [datetime.now() - timedelta(days=x) for x in range(10)]
random_dates = [random.choice(dates) for d in range(number_of_lines)]
random_dates.sort()
with open(filename, 'w') as f:
for d in random_dates:
d = datetime.strftime(d, '%d/%m/%Y:%H:%M:%S')
f.write(f'{random.choice(ips)} - - [{d}] "POST {random.choice(endpoints)} HTTP/1.1" {random.choice(http_codes)} 162 "-" "{random.choice(uas)}"\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment