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 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体