Skip to content

Instantly share code, notes, and snippets.

@huynhbaoan
Last active May 8, 2025 08:44
Show Gist options
  • Save huynhbaoan/a10070997a48ac1faad1f1a5ec0f185a to your computer and use it in GitHub Desktop.
Save huynhbaoan/a10070997a48ac1faad1f1a5ec0f185a to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
set -euo pipefail
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <input_file>"
exit 1
fi
input_file=$1
# Make sure you’ve exported MYPATH before running this script:
# export MYPATH=/path/to/your/repos
if [[ -z ${MYPATH+x} ]]; then
echo "Error: MYPATH environment variable is not set."
exit 1
fi
while IFS=$'\n' read -r env || [[ -n $env ]]; do
# skip empty lines
[[ -z $env ]] && continue
echo "# $env"
dir="$MYPATH/aws_account/account-build/$env"
tf_file="$dir/vlan-variables.tf"
#
# 1) private-vpc-cidr
#
if [[ -f $tf_file ]]; then
priv_cidr=$(
grep -E 'variable "private-vpc-cidr"' "$tf_file" \
| grep -oE 'default = "[^"]+"' \
| sed 's/default = "//; s/"//' \
|| true
)
else
priv_cidr=""
fi
if [[ -n $priv_cidr ]]; then
echo "OR isipv4insubnet (source_ip, \"$priv_cidr\" ) #-> private-vpc-cidr"
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> private-vpc-cidr (not found)"
fi
#
# 2) tooling-vpc-cidr
#
if [[ -f $tf_file ]]; then
tool_cidr=$(
grep -E 'variable "tooling-vpc-cidr"' "$tf_file" \
| grep -oE 'default = "[^"]+"' \
| sed 's/default = "//; s/"//' \
|| true
)
else
tool_cidr=""
fi
if [[ -n $tool_cidr ]]; then
echo "OR isipv4insubnet (source_ip, \"$tool_cidr\" ) #-> tooling-vpc-cidr"
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> tooling-vpc-cidr (not found)"
fi
#
# 3) secondary-cidr-*
#
secondary_files=( "$dir"/secondary-cidr-* )
# check if at least one file exists
if [[ -e ${secondary_files[1]:-} ]]; then
found_sec=false
for f in "${secondary_files[@]}"; do
# extract all "cidr" = "..." lines
while IFS= read -r cidr; do
echo "OR isipv4insubnet (source_ip, \"$cidr\" ) #-> 2nd-subnet-cidr"
found_sec=true
done < <(
grep -oE '"cidr"\s*=\s*"[^"]+"' "$f" \
| sed 's/.*"cidr"\s*=\s*"\([^"]*\)".*/\1/' \
|| true
)
done
if ! $found_sec; then
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> 2nd-subnet-cidr (cidr not found in secondary files)"
fi
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> 2nd-subnet-cidr (secondary-cidr files not found)"
fi
echo
done < "$input_file"
#!/usr/bin/env zsh
set -euo pipefail
setopt nullglob
input_file="${1:-}"
if [[ -z $input_file || ! -f $input_file ]]; then
echo "Usage: $0 <input-file>" >&2
exit 1
fi
while IFS= read -r lz || [[ -n $lz ]]; do
# Print the header for this VPC
echo "# ${lz}"
base="${MYPATH}/aws_account/account-build/${lz}"
if [[ ! -d $base ]]; then
echo "# Directory $base not found"
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> private-vpc-cidr (directory not found)"
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> tooling-vpc-cidr (directory not found)"
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> 2nd-subnet-cidr (directory not found)"
continue
fi
tf="$base/vlan-variables.tf"
# --- private-vpc-cidr ---
if [[ -f $tf ]]; then
private=$(grep -E 'variable "private-vpc-cidr"' "$tf" \
| grep -o 'default *= *"[^"]*"' \
| cut -d'"' -f2)
else
private=""
fi
if [[ -n $private ]]; then
echo "OR isipv4insubnet (source_ip, \"$private\" ) #-> private-vpc-cidr"
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> private-vpc-cidr (not found)"
fi
# --- tooling-vpc-cidr ---
if [[ -f $tf ]]; then
tooling=$(grep -E 'variable "tooling-vpc-cidr"' "$tf" \
| grep -o 'default *= *"[^"]*"' \
| cut -d'"' -f2)
else
tooling=""
fi
if [[ -n $tooling ]]; then
echo "OR isipv4insubnet (source_ip, \"$tooling\" ) #-> tooling-vpc-cidr"
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> tooling-vpc-cidr (not found)"
fi
# --- secondary-cidr-* files ---
sec_files=( "$base"/secondary-cidr-* )
if (( ${#sec_files[@]} )); then
for sec in "${sec_files[@]}"; do
if [[ -f $sec ]]; then
# extract all "cidr" = "x.x.x.x/xx"
cidrs=( $(grep -o '"cidr" *= *"[^"]*"' "$sec" \
| sed 's/.*"cidr" *= *"\([^"]*\)".*/\1/') )
if (( ${#cidrs[@]} )); then
for c in "${cidrs[@]}"; do
echo "OR isipv4insubnet (source_ip, \"$c\" ) #-> 2nd-subnet-cidr"
done
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> 2nd-subnet-cidr (no cidr entries in $(basename "$sec"))"
fi
fi
done
else
echo "#OR isipv4insubnet (source_ip, \"\" ) #-> 2nd-subnet-cidr (secondary-cidr files not found)"
fi
done < "$input_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment