forked from networklore/netrasp
/
sros.go
103 lines (86 loc) · 2.57 KB
/
sros.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package netrasp
import (
"context"
"fmt"
"regexp"
"strings"
)
// SROS is the Netrasp driver for Nokia SR OS (MD-CLI).
type sros struct {
Connection connection
}
// Close connection to device.
func (s sros) Close(ctx context.Context) error {
s.Connection.Close(ctx)
return nil
}
// Configure device.
func (s sros) Configure(ctx context.Context, commands []string) (ConfigResult, error) {
var result ConfigResult
_, err := s.Run(ctx, "edit-config exclusive")
if err != nil {
return result, fmt.Errorf("unable to enter exclusive edit mode: %w", err)
}
for _, command := range commands {
output, err := s.Run(ctx, command)
configCommand := ConfigCommand{Command: command, Output: output}
result.ConfigCommands = append(result.ConfigCommands, configCommand)
if err != nil {
return result, fmt.Errorf("unable to run command '%s': %w", command, err)
}
}
_, err = s.Run(ctx, "commit")
if err != nil {
return result, fmt.Errorf("unable to commit configuration: %w", err)
}
_, err = s.Run(ctx, "quit-config")
if err != nil {
return result, fmt.Errorf("unable to quit from configuration mode: %w", err)
}
return result, nil
}
// Dial opens a connection to a device.
func (s sros) Dial(ctx context.Context) error {
return establishConnection(ctx, s, s.Connection, s.basePrompt(), []string{"environment more false"})
}
// Enable elevates privileges.
func (s sros) Enable(ctx context.Context) error {
return nil
}
// Run executes a command on a device.
func (s sros) Run(ctx context.Context, command string) (string, error) {
output, err := s.RunUntil(ctx, command, s.basePrompt())
if err != nil {
return "", err
}
output = strings.ReplaceAll(output, "\r\n", "\n")
lines := strings.Split(output, "\n")
result := ""
// len-2 to cut off the context piece of the prompt aka [/]
for i := 1; i < len(lines)-2; i++ {
// skip empty lines that sros adds for visual separation of diff commands
// as we add it manually
if (i == len(lines)-3) && (lines[i] == "") {
continue
} else {
result += lines[i] + "\n"
}
}
return result, nil
}
// RunUntil executes a command and reads until the provided prompt.
func (s sros) RunUntil(ctx context.Context, command string, prompt *regexp.Regexp) (string, error) {
err := s.Connection.Send(ctx, command)
if err != nil {
return "", fmt.Errorf("unable to send command to device: %w", err)
}
reader := s.Connection.Recv(ctx)
output, err := readUntilPrompt(ctx, reader, prompt)
if err != nil {
return "", err
}
return output, nil
}
func (s sros) basePrompt() *regexp.Regexp {
return regexp.MustCompile(`^[ABCD]:\S+@\S+#`)
}