我有一堆GCE实例,我想在所有这些实例上运行相同的shell命令 . 是否有可能做 gcloud compute ssh --command="ls -al" my-instance1 my-instance2 my-instance3 这样的事情?
gcloud compute ssh --command="ls -al" my-instance1 my-instance2 my-instance3
您可以使用 gcloud compute instances list --format='value[separator=","](name,zone)' 获取如下列表:
gcloud compute instances list --format='value[separator=","](name,zone)'
my-instance1,my-zone1 my-instance2,my-zone2 my-instance3,my-zone3
然后你可以使用bash Substring Removal来提取逗号前后的部分 .
var="before,after" before="${var%,*}" after="${var#*,}"
把它全部放在循环中并添加尾随'&'以在后台运行:
for instance in $(gcloud compute instances list --format='value[separator=","](name,zone)'); do name="${instance%,*}"; zone="${instance#*,}"; gcloud compute ssh $name --zone=$zone --command="ls -al" & done
1 回答
您可以使用
gcloud compute instances list --format='value[separator=","](name,zone)'
获取如下列表:然后你可以使用bash Substring Removal来提取逗号前后的部分 .
把它全部放在循环中并添加尾随'&'以在后台运行: