6

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

2
  • I've set timeout in ssh.ClientConfig, but i still have infinite delay when ssh server is closed. However, I've found a mistake, done chan int needs to be created though make(chan int) Commented Mar 30, 2017 at 15:22
  • Sorry, I initially misunderstood your question. There is no way to supply a timeout for an ssh direct-tcpip command, 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) Commented Mar 30, 2017 at 15:59

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

1 Comment

direct-tcpip does not support deadlines. sourcegraph.com/github.com/golang/crypto/-/blob/ssh/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.