Golang: Ssh: Handshake Failed: Eof

Golang: Ssh: Handshake Failed: Eof

I wrote a go project to deploy online code. I need to remote machine and run some start reload like command.

I have 200+ machines, so I use go routine to do this job.

The problem is sometimes ssh failure throw ssh: handshake failed: EOF or ssh: handshake failed: read tcp 10.19.177.216:44721->10.19.139.36:22: read: connection reset by peer why ?

My core code:

func RunRemoteCmd(selfDesc string, host string, cmd string, ch chan<- RunResult) {
    startTime := time.Now()
    sshConfig := &ssh.ClientConfig{
        User: "root",
        Auth: []ssh.AuthMethod{
            publicKey,
        },
        HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
            return nil
        },
    }

    addr := fmt.Sprintf("%s:22", host)
    connection, err := ssh.Dial("tcp", addr, sshConfig)
    if err != nil {
        ch <- RunResult{selfDesc, err.Error(), "", time.Since(startTime)}
        return
    }

    session, err := connection.NewSession()
    if err != nil {
        ch <- RunResult{selfDesc, err.Error(), "", time.Since(startTime)}
        return
    }
    defer session.Close()

    var out bytes.Buffer
    session.Stdout = &out

    session.Run(cmd)
    ch <- RunResult{selfDesc, "", out.String(), time.Since(startTime)}
}
1

1 Answer

Maybe multiple ssh connection refuse by remote server.

I change one server one connection and muliple NewSession fixed this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.