'Failed to dial when trying to create a session

I try to connect to the host via ssh using a private key. I use an example from this documentation: https://pkg.go.dev/golang.org/x/crypto/ssh#PublicKeys

func main() {
    var hostKey ssh.PublicKey
    // A public key may be used to authenticate against the remote
    // server by using an unencrypted PEM-encoded private key file.
    //
    // If you have an encrypted private key, the crypto/x509 package
    // can be used to decrypt it.
    key, err := ioutil.ReadFile("/home/root/.ssh/id_rsa")
    if err != nil {
        log.Fatalf("unable to read private key: %v", err)
    }

    // Create the Signer for this private key.
    signer, err := ssh.ParsePrivateKey(key)
    if err != nil {
        log.Fatalf("unable to parse private key: %v", err)
    }

    config := &ssh.ClientConfig{
        User: "user",
        Auth: []ssh.AuthMethod{
            // Use the PublicKeys method for remote authentication.
            ssh.PublicKeys(signer),
        },
        HostKeyCallback: ssh.FixedHostKey(hostKey),
    }

    // Connect to the remote server and perform the SSH handshake.
    client, err := ssh.Dial("tcp", "host.com:22", config)
    if err != nil {
        log.Fatalf("unable to connect: %v", err)
    }
    defer client.Close()
}

And I've got an error:

Failed to dial: dial tcp 10.1.12.10:22: connect: operation timed out

I can connect to this host from the terminal on my mac so with a network I think it all is good.

Maybe someone had a similar issue, will appreciate any help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source