-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexample_ssh_test.go
More file actions
51 lines (41 loc) · 1.01 KB
/
example_ssh_test.go
File metadata and controls
51 lines (41 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package netconf_test
import (
"context"
"log"
"time"
"golang.org/x/crypto/ssh"
"nemith.io/netconf"
"nemith.io/netconf/rpc"
ncssh "nemith.io/netconf/transport/ssh"
)
const sshAddr = "myrouter.example.com:830"
func Example_ssh() {
config := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{
ssh.Password("secret"),
},
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
transport, err := ncssh.Dial(ctx, "tcp", sshAddr, config)
if err != nil {
panic(err)
}
defer transport.Close() // nolint:errcheck
session, err := netconf.NewSession(transport)
if err != nil {
panic(err)
}
// timeout for the call itself.
ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deviceConfig, err := rpc.GetConfig{Source: rpc.Running}.Exec(ctx, session)
if err != nil {
log.Fatalf("failed to get config: %v", err)
}
log.Printf("Config:\n%s\n", deviceConfig)
if err := session.Close(context.Background()); err != nil {
log.Print(err)
}
}