title: golang: ssh: handshake failed: EOF
body: <p>I wrote a go project to deploy online code. I need to remote machine and run some <code>start</code> <code>reload</code> like command.</p>
  
  <p>I have 200+ machines, so I use go routine to do this job.</p>
  
  <p>The problem is <strong>sometimes</strong> ssh failure throw <code>ssh: handshake failed: EOF</code> or <code>ssh: handshake failed: read tcp 10.19.177.216:44721-&gt;10.19.139.36:22: read: connection reset by peer</code> why ?</p>
  
  <p>My core code:</p>
  
  <pre><code>func RunRemoteCmd(selfDesc string, host string, cmd string, ch chan&lt;- RunResult) {
      startTime := time.Now()
      sshConfig := &amp;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 &lt;- RunResult{selfDesc, err.Error(), "", time.Since(startTime)}
          return
      }
  
      session, err := connection.NewSession()
      if err != nil {
          ch &lt;- RunResult{selfDesc, err.Error(), "", time.Since(startTime)}
          return
      }
      defer session.Close()
  
      var out bytes.Buffer
      session.Stdout = &amp;out
  
      session.Run(cmd)
      ch &lt;- RunResult{selfDesc, "", out.String(), time.Since(startTime)}
  }
  </code></pre>