Skip to content

Instantly share code, notes, and snippets.

@codebymikey
Created June 6, 2025 08:57
Show Gist options
  • Select an option

  • Save codebymikey/5b36aef3b7210dd54608e6a917bda63d to your computer and use it in GitHub Desktop.

Select an option

Save codebymikey/5b36aef3b7210dd54608e6a917bda63d to your computer and use it in GitHub Desktop.
Script to generate an nginx deny rule for a specific ASN
#!/usr/bin/env bash
# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset
# Script to generate deny rules for a specific ASN, e.g. Alibaba's https://ipinfo.io/AS45102.
# This provides a more up to date rule set than relying on a separate external tool.
ASN="${1?you must specify the ASN}"
whois -h whois.radb.net -- "-i origin $ASN" > "$ASN.txt"
sed -nE 's/route:\s*([0-9]{1,3}(\.[0-9]{1,3}){3}\/[0-9]+).*/deny \1;/p' "$ASN.txt" > "deny_$ASN.conf"
# Remove duplicates.
awk -i inplace '!seen[$0]++' "deny_$ASN.conf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment