0

I need to create a custom timeout solution for ssh.Dial. I tried setting the timeout in the sshConfig, but it just doesn't work sometimes which causes the whole program to hang.

connection, err := ssh.Dial("tcp", "X.X.X.X:22", sshConfig)

In some cases it times out, it works in other cases. But it stumbles on specific IPs and doesn't do anything. It just hangs the entire program.

So how would I go about coding "my own" timeout solution for this line of code?

2
  • instead of using ssh.Dial use DialTimeout e.g :- listener, err := net.DialTimeout("tcp", "192.xxx.x.xxx:8899", 10*time.Second) if err != nil { fmt.Printf("connection error ", err.Error()) } DialTimeout acts like Dial but takes a timeout. Commented Jan 2, 2018 at 6:48
  • I am curious at what makes the code hang. Would you please provide some more info on the situation? Commented Jan 2, 2018 at 9:24

2 Answers 2

1

It looks like the config takes a timeout. See: https://godoc.org/golang.org/x/crypto/ssh#ClientConfig

in other words:

conn, err := ssh.Dail("tcp", "X.X.X.X:22", ssh.ClientConfig{Timeout: time.Second * 4})
Sign up to request clarification or add additional context in comments.

1 Comment

It takes a timeout. It just doesn't respect it and will hang on some IPs indefinitely.
0

Did this and it worked:

    c1 := make(chan bool, 1)
    go RunRequest(element, authInfo, c1)
    select {
        case <-c1:
        case <-time.After(time.Second * 4):
    }

The ssh.Dial was inside the RunRequest GoRoutine.

2 Comments

That doesn't timeout anything, and just leaves the hanging function running indefinitely, which may or may not be tying up system resources.
But it’s not “timing out” RunRequest if it’s blocked, it leaves it blocked indefinitely. If the timeout isn’t working, figure what’s blocked rather than ignoring it.

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.