golang:ssh:握手失败:EOF [英] golang: ssh: handshake failed: EOF
问题描述
我写了一个go项目来部署在线代码.我需要远程计算机并运行一些 start
reload
之类的命令.
I wrote a go project to deploy online code. I need to remote machine and run some start
reload
like command.
我有200多台机器,所以我使用go例程来完成这项工作.
I have 200+ machines, so I use go routine to do this job.
问题是有时 ssh故障抛出 ssh:握手失败:EOF
或 ssh:握手失败:阅读tcp 10.19.177.216:44721->10.19.139.36:22:阅读:对等连接重置
为什么?
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 ?
我的核心代码:
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)}
}
推荐答案
也许远程服务器拒绝了多个ssh连接.
Maybe multiple ssh connection refuse by remote server.
我将一台服务器更改为 connection
,并将多个 NewSession
修复了此问题.
I change one server one connection
and muliple NewSession
fixed this problem.
这篇关于golang:ssh:握手失败:EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!