Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang: ssh: handshake failed: EOF

Tags:

ssh

go

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)}
}
like image 387
Jerry Zhang Avatar asked Nov 08 '22 13:11

Jerry Zhang


1 Answers

Maybe multiple ssh connection refuse by remote server.

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

like image 76
Jerry Zhang Avatar answered Nov 14 '22 23:11

Jerry Zhang