Skip to content

Instantly share code, notes, and snippets.

@quangnhut123
Created September 14, 2023 07:08
Show Gist options
  • Save quangnhut123/4c0535cf19c72615f444f0b72f9427d0 to your computer and use it in GitHub Desktop.
Save quangnhut123/4c0535cf19c72615f444f0b72f9427d0 to your computer and use it in GitHub Desktop.
import os
import yaml
# Define the folder path containing the YAML files
folder_path = (
"YOUR_BASE_YAML_FOLDER_PATH"
)
# Initialize variables to store the largest offset and access port
largest_offset = -1
largest_access_port = -1
# Prompt the user for the CIDR block they want to check
target_cidr_block = input("Enter the CIDR block to check (e.g., 10.32.160.0/21): ")
# Function to load YAML data from a file
def load_yaml(file_path):
try:
with open(file_path, "r") as yaml_file:
return yaml.safe_load(yaml_file)
except yaml.YAMLError as e:
print(f"Error loading YAML from {file_path}: {e}")
return None
# Function to get values from nested dictionaries
def get_nested_value(data, keys, default=None):
for key in keys:
data = data.get(key, {})
if not data:
return default
return data
# Iterate through the YAML files in the folder
for filename in os.listdir(folder_path):
if filename.endswith(".yaml") and filename != "sample-baseyaml.yaml":
yaml_path = os.path.join(folder_path, filename)
data = load_yaml(yaml_path)
if not data:
continue
# Access the relevant data in a more concise manner
bastion_data = get_nested_value(data, ["spec", "aws", "bastion"], {})
cidr_block = bastion_data.get("cidrBlock", "")
access_port = bastion_data.get("accessPort", -1)
# Check if the CIDR block matches the target
offset = (
bastion_data.get("offset", -1) if cidr_block == target_cidr_block else -1
)
# Update the largest offset and access port
largest_offset = max(largest_offset, offset)
largest_access_port = max(largest_access_port, access_port)
if largest_offset != -1:
# Calculate the next usable offset and access port
next_usable_offset = largest_offset + 3
next_usable_access_port = largest_access_port + 1
print(f"Usable Offset: {next_usable_offset}")
print(f"Usable Access Port: {next_usable_access_port}")
else:
print("No CIDR found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment