Created
July 5, 2020 19:29
-
-
Save cjsewell/cdf926cd70bd17288eeefcb46327376a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
RULE_NAMES=$(gcloud compute firewall-rules list --format json --project guru-cloud-hosting | jq ".[].name") | |
RULE_COUNT=1 | |
COUNT=1 | |
IPS=() | |
while read LINE; do | |
IPS+=("${LINE}") | |
((COUNT=COUNT+1)) | |
if [[ "${COUNT}" -eq 256 ]]; then | |
LIST=$(IFS=, ; echo "${IPS[*]}") | |
RULE_NAME="ddossource${RULE_COUNT}" | |
if echo "${RULE_NAMES}" | grep -q "${RULE_NAME}" ; then | |
echo "Update ${RULE_NAME}" | |
gcloud compute firewall-rules update "${RULE_NAME}" --project guru-cloud-hosting --source-ranges="${LIST}" | |
else | |
echo "Create ${RULE_NAME}" | |
gcloud compute firewall-rules create "${RULE_NAME}" --project guru-cloud-hosting --source-ranges="${LIST}" --direction=INGRESS --action deny --rules all | |
fi; | |
((RULE_COUNT=RULE_COUNT+1)) | |
COUNT=1 | |
IPS=() | |
fi; | |
done < toBlock.txt | |
if [[ " ${#IPS[@]}" -gt 0 ]]; then | |
LIST=$(IFS=, ; echo "${IPS[*]}") | |
RULE_NAME="ddossource${RULE_COUNT}" | |
if echo "${RULE_NAMES}" | grep -q "${RULE_NAME}" ; then | |
echo "Update ${RULE_NAME}" | |
gcloud compute firewall-rules update "${RULE_NAME}" --project guru-cloud-hosting --source-ranges="${LIST}" | |
else | |
echo "Create ${RULE_NAME}" | |
gcloud compute firewall-rules create "${RULE_NAME}" --project guru-cloud-hosting --source-ranges="${LIST}" --direction=INGRESS --action deny --rules all | |
fi; | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment