golang ssh拨号超时

标签 go ssh

在 ssh 连接上创建 DialTimeout 的最佳方法是什么?例如,这段代码总是返回“Ping deadline exceed”:

func (t *Tunnel) ping(sshConn *ssh.Client, addr string) (net.Conn, error) {
    var (
        conn net.Conn
        err  error
        done chan int
    )
    go func() {
        time.Sleep(getSeconds(10))
        err = errors.New("Ping deadline exceed")
        log.Printf("%v\nStatus: bad %s -> %s", err, t.serverAddr, addr)
        t.writeStatus(bad)
        done <- 1
        close(done)
    }()
    go func() {
        conn, err = sshConn.Dial("tcp", addr)
        if err != nil {
            t.writeStatus(bad)
            log.Printf("%v\nStatus: bad %s -> %s", err, t.serverAddr, addr)
        }
        done <- 1
        close(done)
    }()
    <-done
    return conn, err
}

ssh.ClientConfig 中的 PS 超时设置为 5 秒

最佳答案

It's been two years but, may someone need the solution. So here it is.

在ssh.Dial中,你可以通过ssh.ClientConfig进行配置,

config := &ssh.ClientConfig { Timeout: time.Minute }
client, _ := ssh.Dial("tcp", t.ServerAddr, config)

您可以在 here 中找到更多信息:

// A Timeout of zero means no timeout. Timeout time.Duration

关于golang ssh拨号超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43116757/

相关文章:

ubuntu - 在 Ansible 中配置 SSH

git - Jenkins 未能从 GitHub 获取私有(private)仓库

java - Jsch ChannelExec : How to pass a password to server after the command?

Golang 混合赋值和声明

go - MongoDB 在存储值之前未对其进行编码

linux - 并行执行多个 scps 时,为什么 scp 偶尔会失败?

bash - 通过 ssh 的变量

reflection - Reflect.Value.FieldByName 导致 panic

json - 使用 Go 访问嵌套数组和对象中的数据

testing - Go包之间如何共享测试接口(interface)?