-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_ssh_test.go
50 lines (40 loc) · 977 Bytes
/
example_ssh_test.go
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
package netconf_test
import (
"context"
"log"
"time"
"github.com/nemith/netconf"
ncssh "github.com/nemith/netconf/transport/ssh"
"golang.org/x/crypto/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()
session, err := netconf.Open(transport)
if err != nil {
panic(err)
}
// timeout for the call itself.
ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deviceConfig, err := session.GetConfig(ctx, "running")
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)
}
}