【问题标题】:GCloud ssh cannot connect when calledGCloud ssh 调用时无法连接
【发布时间】:2022-01-06 07:29:25
【问题描述】:

我在 Go 中有测试代码,它在 Compute Engine 实例上设置状态(并且不想公开端口)。 由于似乎没有直接在 Golang 中执行此操作的简单方法,因此我使用gcloud

func TestMe(t *testing.T) {
  cmd := exec.Command("gcloud", "compute", "ssh", "--project", "foo", "--tunnel-through-iap", "--zone", "europe-west1-z", "bar", `--ssh-flag="-T"`, "--", "echo WOW")
  cmd.Env = os.Environ()
  out, err := cmd.CombinedOutput()
  println(string(out))
  t.Fatal(err)
}

运行go test 时,cmd 输出:

bash: myuser@compute.123456789: command not found

计算实例正确 - 但尝试访问计算实例时出现故障。相同的 gcloud 命令适用于 bash(即使对于非交互式)。我认为 Golang 环境中有些不同。

【问题讨论】:

  • 你能显示失败的代码吗? TestMe 正文似乎并不完整。输出错误的代码在哪里?请务必在打印错误的行之前包含代码。
  • 问题是 gcloud 是否会隐式生成这样的脚本/二进制文件。我隐约记得过去曾发生过类似的错误 - 但不是导致它的原因。
  • 删除 --ssh-flag="-T" 似乎可以正常工作。 -T-t 都失败了?
  • 你试过"--ssh-flag='-T'"吗?只是好奇它是否与 shell 如何处理双引号字符串无关。
  • 试试"--ssh-flag", "-T"

标签: go google-compute-engine gcloud google-iap identity-aware-proxy


【解决方案1】:

代码中有一个小的更正。

更正的代码:

func TestMe(t *testing.T)
 {
 cmd := exec.Command("gcloud", "compute", "ssh", "--project", "foo", "--tunnel-through-iap", "--zone", "europe-west1-z", "bar", "--ssh-flag=-T", "--", "echo WOW") 
cmd.Env = os.Environ()
 out, err := cmd.CombinedOutput() 
println(string(out)) 
t.Fatal(err) 
}

有关解决 ssh 问题的更多信息,您可以参考documentation

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 2018-10-28
    • 2020-08-29
    • 2019-08-19
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多