Skip to content

Instantly share code, notes, and snippets.

@ushitora-anqou
Last active October 31, 2022 04:49
Show Gist options
  • Select an option

  • Save ushitora-anqou/527197da0b2d99dfb35cc4ab3cdfd974 to your computer and use it in GitHub Desktop.

Select an option

Save ushitora-anqou/527197da0b2d99dfb35cc4ab3cdfd974 to your computer and use it in GitHub Desktop.
GCPのファイアウォールに中国とオーストラリアからの上り通信を弾く設定を追加するシェルスクリプト
#!/bin/bash
#
# Shell script to add GCP's firewall to deny all ingress from China and Australia.
# Author: Ushitora Anqou
# License: MIT
#
curl https://ipv4.fetus.jp/cn.txt | grep -vE '#|^$' > cn.txt
curl https://ipv4.fetus.jp/au.txt | grep -vE '#|^$' > au.txt
for i in $(seq 1 250 $(wc -l cn.txt | cut -d' ' -f1)); do sed -n "$i,$((249 + i))p" cn.txt > cn-$(echo $i | awk '{printf "%02d", ($1-1)/250}'); done
for i in $(seq 1 250 $(wc -l au.txt | cut -d' ' -f1)); do sed -n "$i,$((249 + i))p" au.txt > au-$(echo $i | awk '{printf "%02d", ($1-1)/250}'); done
ls cn-* au-* | while read fn; do
gcloud compute firewall-rules create "deny-$fn" --action deny --network default --priority 100 --rules all\
--source-ranges $(paste -s -d ',' $fn)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment