@@ -7,6 +7,7 @@ package ssh_test
77import (
88 "bufio"
99 "bytes"
10+ "context"
1011 "crypto/rand"
1112 "crypto/rsa"
1213 "fmt"
@@ -17,6 +18,7 @@ import (
1718 "path/filepath"
1819 "strings"
1920 "sync"
21+ "time"
2022
2123 "golang.org/x/crypto/ssh"
2224 "golang.org/x/crypto/ssh/terminal"
@@ -262,6 +264,30 @@ func ExampleDial() {
262264 fmt .Println (b .String ())
263265}
264266
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+
265291func ExamplePublicKeys () {
266292 var hostKey ssh.PublicKey
267293 // A public key may be used to authenticate against the remote
0 commit comments