GCloud ssh cannot connect when called

313 views Asked by At

I have testing code in Go, which sets up a state on a Compute Engine Instance (and don't want to expose the ports). Since there seems to be no easy way of doing this in Golang directly, I utilize 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)
}

When running go test, cmd outputs:

bash: [email protected]: command not found

The compute instance is correct - but something fails when trying to access the compute instance. The same gcloud command works in bash (even for non-interactive). I assume there is something different in the Golang environment.

1

There are 1 answers

0
Divyani Yadav On

There is a small correction in the code.

corrected code:

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) 
}

For more information related to troubleshooting ssh issues you can refer to the documentation.