GCP 计算引擎 ssh 仅在文件内部失败

GCP Compute-engine ssh fails only inside files

我正在尝试编写启动 VM 的脚本,运行 一个命令然后停止它。

代码为(省略项目和区域):

gcloud compute instances start instance-1
gcloud compute ssh user@instance-1 --command 'echo check-ssh' -- -vvv
gcloud compute instances stop instance-1

当我 运行 those 进入终端时一切顺利,但是当我 运行 它通过文件时,ssh 命令失败,我得到:

OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 34.68.81.200 is address
debug2: ssh_connect_direct
debug1: Connecting to 34.68.81.200 [34.68.81.200] port 22.
debug1: connect to address 34.68.81.200 port 22: Connection refused
ssh: connect to host 34.68.81.200 port 22: Connection refused
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].

实例 os 是 ubuntu 20.4.

可能与此问题有关,当我尝试通过终端连接到 root 用户时,我遇到了同样的错误。

有人知道有什么区别吗?我该如何解决?

当您请求 GCP 计算实例的 gcloud 计算实例启动时,您实际上是在请求它启动,而不是它实际上已经启动。如果您随后转身执行 SSH 请求,则 VM 可能尚未达到准备好接收 SSH 请求的内部状态。尝试在您的 VM 启动和 SSH 访问之间设置一个睡眠延迟(比如... 5 分钟)...看看这是否会改变故事。

另请参阅: