terratest icon indicating copy to clipboard operation
terratest copied to clipboard

SSH to public host "hostname" returned an error: dial tcp: lookup "hostname": no such host. Sleeping for 5s and will try again

Open aknirak opened this issue 3 years ago • 10 comments

Getting this error using the ssh go script. Any ideas on how to resolve this ?

SH to public host "hostip" returned an error: dial tcp: lookup "hostip": no such host. Sleeping for 5s and will try again

aknirak avatar Feb 26 '21 14:02 aknirak

I am also facing similar issue

end2end_test.go:80: Cannot establish SSH connection to vm-linux-1 public IP address: dial tcp: lookup "52.148.xxx.xx": no such host

ameet-nandihal avatar Mar 08 '21 07:03 ameet-nandihal

Impossible to say without more context: i.e., your implementation code, your test code, the full log output. Without that, the only thing I can do is blindly guess: e.g., if you happen to be trying to SSH to an EC2 instance you just deployed (e.g. via terraform apply), then it takes ~1 minute for the instance to boot and be able to accept SSH connections, and until then, you'll get connection errors. Therefore, we typically do SSH connections in a retry loop using the retry package.

brikis98 avatar Mar 09 '21 10:03 brikis98

I am facing something similar...

func executeCmd(t *testing.T, command, hostname string, port string, config *ssh.ClientConfig) string {
	conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", hostname, port), config)
	if err != nil {
		t.Fatalf("Cannot establish SSH connection to the vm public IP address: %v", err)
	}
...

Called with:

output := executeCmd(t, command, vmPublicIPAddress, "22", sshConfig)

The VM is up and running, I can ssh into without problems. Yet Terratest says:

--- FAIL: TestEndToEndDeploymentScenario (1129.75s)
    end2end_test.go:86: Cannot establish SSH connection to the vm public IP address: dial tcp: lookup "40.113.19.17": no such host

tomconte avatar Apr 07 '21 14:04 tomconte

Similar code, executed outside of Terratest, works fine:

package main

import (
	"bytes"
	"fmt"

	"golang.org/x/crypto/ssh"
)

func main() {
	kkk := `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`
	signer, err := ssh.ParsePrivateKey([]byte(kkk))
	if err != nil {
		fmt.Printf("Unable to parse key: %v", err)
	}

	sshConfig := &ssh.ClientConfig{
		User:            "azureuser",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}

	conn, err := ssh.Dial("tcp", "40.113.19.17:22", sshConfig)

	if err != nil {
		fmt.Printf("Cannot establish SSH connection to the vm public IP address: %v", err)
	}

	session, err := conn.NewSession()
	if err != nil {
		fmt.Printf("Cannot create SSH session to the vm public IP address: %v", err)
	}

	defer session.Close()
	defer conn.Close()

	var stdoutBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	err = session.Run("uname -a")
	if err != nil {
		fmt.Printf("Cannot execute SSH command: %v", err)
	}

	fmt.Printf(stdoutBuf.String())
}

tomconte avatar Apr 07 '21 14:04 tomconte

This could be a bug in Terratest, or perhaps some issue with the way Go resolves DNS. @anouarchattouna, I think you hit something similar... Did you ever get it figured out?

brikis98 avatar Apr 08 '21 08:04 brikis98

This could be a bug in Terratest, or perhaps some issue with the way Go resolves DNS. @anouarchattouna, I think you hit something similar... Did you ever get it figured out?

Unfortunately not, I switched to a fresh environment with recent version of Go and I had no error

anouarchattouna avatar Apr 08 '21 09:04 anouarchattouna

Same here, I updated my dependencies to the latest version of the Terratest module and also upgraded from Go 1.15 to 1.16. It's working fine now...

tomconte avatar Apr 08 '21 10:04 tomconte

After updating the Go version i am getting same error message: " Cannot establish SSH connection to vm-linux-1 public IP address: dial tcp: lookup XX.XX.XX.XX: no such host.

tmitra1990 avatar May 24 '21 18:05 tmitra1990

Same here, I updated my dependencies to the latest version of the Terratest module and also upgraded from Go 1.15 to 1.16. It's working fine now...

@tomconte do you remember which specific Go version 1.16.x worked for you? Currently having the same problem with go 1.17.1 and planning to downgrade to a stable 1.16.x version. This is the error message I'm getting: "end2end_test.go:83: Cannot establish SSH connection to vm-linux-1 public IP address: dial tcp: lookup "XX.XXX.XX.XXX": no such host"

ayildirimnec avatar Sep 27 '21 23:09 ayildirimnec

any fix for this?

changan1111 avatar Dec 22 '22 12:12 changan1111