Golang: ssh: handshake failed: EOF

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

I have 200 more cars, so I use a routine to complete this task.

Problem sometimes ssh fail throw ssh: handshake failed: EOFor ssh: handshake failed: read tcp 10.19.177.216:44721->10.19.139.36:22: read: connection reset by peerwhy?

My main code is:

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)}
}
+4
source share
1 answer

Perhaps several remote ssh connections by a remote server.

I change one server to one connectionand muliple NewSessionfixed this problem.

+1
source

Source: https://habr.com/ru/post/1690753/


All Articles