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

问题描述

我知道可以像这样使用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到实例,您就应该能够:

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...