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)}
}
Maybe multiple ssh connection refuse by remote server.
I change one server one connection
and muliple NewSession
fixed this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With