@@ -7,6 +7,7 @@ package ssh_test
7
7
import (
8
8
"bufio"
9
9
"bytes"
10
+ "context"
10
11
"crypto/rand"
11
12
"crypto/rsa"
12
13
"fmt"
@@ -17,6 +18,7 @@ import (
17
18
"path/filepath"
18
19
"strings"
19
20
"sync"
21
+ "time"
20
22
21
23
"golang.org/x/crypto/ssh"
22
24
"golang.org/x/crypto/ssh/terminal"
@@ -262,6 +264,30 @@ func ExampleDial() {
262
264
fmt .Println (b .String ())
263
265
}
264
266
267
+ func ExampleDialContext () {
268
+ var hostKey ssh.PublicKey
269
+ config := & ssh.ClientConfig {
270
+ User : "username" ,
271
+ Auth : []ssh.AuthMethod {
272
+ ssh .Password ("yourpassword" ),
273
+ },
274
+ HostKeyCallback : ssh .FixedHostKey (hostKey ),
275
+ }
276
+
277
+ // The Context supplied to DialContext allows the caller to control
278
+ // the timeout or cancel opening an SSH connection.
279
+ //
280
+ // Cancelling the context after DialContext returns will not effect
281
+ // the resulting Client.
282
+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
283
+ defer cancel ()
284
+ client , err := ssh .DialContext (ctx , "tcp" , "yourserver.com:22" , config )
285
+ if err != nil {
286
+ log .Fatal ("Failed to dial: " , err )
287
+ }
288
+ defer client .Close ()
289
+ }
290
+
265
291
func ExamplePublicKeys () {
266
292
var hostKey ssh.PublicKey
267
293
// A public key may be used to authenticate against the remote
0 commit comments