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\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
}
PS Timeout in ssh.ClientConfig is set to 5 seconds
done chan intneeds to be created thoughmake(chan int)direct-tcpipcommand, but the remote host should be able to timeout eventually, though probably not in under 10 seconds (depends on the remote hosts kernel TCP settings)