【问题标题】:Is there a way to execute commands remotely using gcloud compute ssh utility有没有办法使用 gcloud compute ssh 实用程序远程执行命令
【发布时间】:2024-01-21 11:17:01
【问题描述】:

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

【问题讨论】:

    标签: ssh virtual-machine gcloud private-network


    【解决方案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
    *
    

    【讨论】:

      最近更新 更多