0

我知道可以像这样使用 ssh(在文件中)远程执行命令;

#!/bin/sh

ssh ${USER}@${SERVER_IP} <<EOF
 cd ${PROJECT_PATH}
 git pull
 exit
EOF

我的问题是:gcloud compute ssh 可以做同样的事情吗?我是说;

gcloud compute ssh vm-internal --zone us-central1-c --tunnel-through-iap << EOF
...
...
EOF

PS:该实例是私有的,没有外部 IP,因此使用 iap

4

1 回答 1

1

只要您可以 ssh 到实例上,您就应该能够:

gcloud compute ssh .... --command="bash -s" <<EOF
echo "Hello Freddie"
ls -l
EOF

您可以对此进行测试(感谢gcloud使用 Cloud Shell 的一致性):

gcloud alpha cloud-shell ssh --command="bash -s" <<EOF
echo "Hello Freddie"
ls
EOF

注意你可能不需要,-s但我认为它是首选

产量:

Automatic authentication with GCP CLI tools in Cloud Shell is disabled. To enable, please rerun command with `--authorize-session` flag.
Hello Freddie
Projects
README-cloudshell.txt -> /google/devshell/README-cloudshell.txt
stackoverflow
于 2020-09-11T22:38:07.360 回答