Google compute engine 跨所有gce实例运行ssh命令

Google compute engine 跨所有gce实例运行ssh命令,google-compute-engine,Google Compute Engine,我有一堆GCE实例,我想对它们运行相同的shell命令。是否可以执行gcloud compute ssh-command=ls-al my-instance1 my-instance2 my-instance3之类的操作?您可以使用gcloud compute instances list-format='value[separator=,]name,zone'获得如下列表: my-instance1,my-zone1 my-instance2,my-zone2 my-instance3,my-

我有一堆GCE实例,我想对它们运行相同的shell命令。是否可以执行gcloud compute ssh-command=ls-al my-instance1 my-instance2 my-instance3之类的操作?

您可以使用gcloud compute instances list-format='value[separator=,]name,zone'获得如下列表:

my-instance1,my-zone1
my-instance2,my-zone2
my-instance3,my-zone3
然后可以使用bash提取逗号前后的部分

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
要添加到,以下是如何在不删除子字符串的情况下执行相同操作:

for i z in $(gcloud compute instances list --format='value(name, zone)'); do 
  gcloud compute ssh $i --command="ls -lah" --zone=$z; 
done