Reputation: 403
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
Upvotes: 6
Views: 4527
Reputation: 53
It's been two years but, may someone need the solution. So here it is.
In ssh.Dial, you can give configuration via ssh.ClientConfig,
config := &ssh.ClientConfig { Timeout: time.Minute }
client, _ := ssh.Dial("tcp", t.ServerAddr, config)
You can find more in here:
// A Timeout of zero means no timeout. Timeout time.Duration
Upvotes: 5
Reputation: 6470
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.
Upvotes: -1