如何使用gcloud CLI更新防火墙规则以将新IP地址附加到源范围?

问题描述 投票:0回答:1

我在GCP项目中已有一条防火墙规则,该规则允许传入针对特定IP地址/ IP范围的特定端口的流量。我想更新源范围以包括更多IP地址/ IP范围。我有多个此类防火墙规则。

我可以从用户界面更新防火墙规则,但这很慢,需要多次单击。相反,我想使用gcloud CLI。

这是命令:

gcloud compute firewall-rules update <Firewall rule name> --source-range=<IP addr/range>

尽管这些保留了其余属性(例如,端口列表)(如文档所述)不变,但它会覆盖现有的源IP地址/范围。有没有一种方法可以将IP地址/范围附加到现有的源范围。

我可以执行多步骤过程/脚本,首先使用以下方法检索列表:

gcloud compute firewall-rules list --filter="name=<Firewall rule name>" --format json

并创建更新的列表,然后调用更新命令。

但是假设添加/删除IP地址范围是相当普遍的用例,我想知道是否存在直接的方法。

任何帮助将不胜感激。

谢谢。

google-cloud-platform gcloud firewall
1个回答
0
投票

这是我的脚本,但我希望有一种更简单/直接的方法:

gcloud auth login

p=<gcp-project-name>

x=<firewall-rule-name>

# The command below captures the existing source ip addresses/ip ranges
y=`gcloud compute firewall-rules list --filter="name=$x" --project=$project --format json | sed -n '/sourceRanges/,/]/p' | grep "\d" | xargs |  sed -e 's/ //g'`

z=<new-ip-addr-to-be-updated>

echo "Updating $x in $p with \"$y,$z\""

gcloud compute firewall-rules update $x --project=$p --source-ranges="$y,$z"
© www.soinside.com 2019 - 2024. All rights reserved.