doushi5117 2017-03-30 11:36
浏览 200

golang ssh拨号超时

What is the best way to create DialTimeout on ssh connection? For example, this code always returns "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
Status: 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
Status: bad %s -> %s", err, t.serverAddr, addr)
        }
        done <- 1
        close(done)
    }()
    <-done
    return conn, err
}

PS Timeout in ssh.ClientConfig is set to 5 seconds

  • 写回答

1条回答 默认 最新

  • dougang1605 2017-05-12 06:44
    关注

    Instead of using sshConn.Dial take a look at:

    func DialTimeout(network, address string, timeout time.Duration) (Conn, error)
    

    From the connection docs:

    DialTimeout acts like Dial but takes a timeout. The timeout includes name resolution, if required.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料