Skip to content

Instantly share code, notes, and snippets.

@vchrombie
Created September 24, 2024 14:57
Show Gist options
  • Save vchrombie/91885dede7c21b3dee6fa8421f130db0 to your computer and use it in GitHub Desktop.
Save vchrombie/91885dede7c21b3dee6fa8421f130db0 to your computer and use it in GitHub Desktop.
Generates random flow log data (v2)
import csv
import random
import string
# Load protocols from protocols.csv
protocols = []
with open('protocols.csv', 'r') as protocols_file:
reader = csv.DictReader(protocols_file)
for row in reader:
protocols.append({'number': row['number'], 'name': row['name']})
def generate_ip():
# Function to generate random IP addresses
return '.'.join(str(random.randint(0, 255)) for _ in range(4))
def generate_eni():
# Function to generate random ENI IDs
return 'eni-' + ''.join(random.choices('0123456789abcdef', k=8))
# Generate lookup.csv with over 10,000 mappings
with open('lookup.csv', 'w', newline='') as lookup_file:
fieldnames = ['dstport', 'protocol', 'tag']
writer = csv.DictWriter(lookup_file, fieldnames=fieldnames)
writer.writeheader()
for _ in range(10000):
dstport = random.randint(1, 200)
protocol = random.choice(protocols)['name']
tag = 'tag_' + \
''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
writer.writerow({'dstport': dstport, 'protocol': protocol, 'tag': tag})
# Generate flowlogs.txt up to 10 MB
with open('flowlogs.txt', 'w') as flowlogs_file:
total_size = 0
max_size = 10 * 1024 * 1024 # 10 MB
while total_size < max_size:
# Randomize values
version = 2
account_id = ''.join(random.choices(string.digits, k=12))
eni_id = generate_eni()
srcaddr = generate_ip()
dstaddr = generate_ip()
srcport = random.randint(1, 65535)
dstport = random.randint(1, 200)
protocol_entry = random.choice(protocols)
protocol_num = protocol_entry['number']
packets = random.randint(1, 100)
bytes_transferred = packets * random.randint(40, 1500)
start_time = random.randint(1727104070, 1727204070)
end_time = start_time + random.randint(1, 600)
action = random.choice(['ACCEPT', 'REJECT'])
log_status = 'OK'
# Create log entry
log_entry = f"{version} {account_id} {eni_id} {srcaddr} {dstaddr} {dstport} {srcport} {protocol_num} {packets} {bytes_transferred} {start_time} {end_time} {action} {log_status}\n"
# Write to file and update total_size
flowlogs_file.write(log_entry)
total_size += len(log_entry.encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment