有没有一种方法可以使用gcloud compute ssh实用程序远程执行命令

时间:2020-09-11 21:03:59

标签: ssh virtual-machine gcloud private-network

我知道可以像这样使用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

1 个答案:

答案 0 :(得分: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
相关问题