-
Notifications
You must be signed in to change notification settings - Fork 0
/
fscom.go
192 lines (152 loc) · 4.58 KB
/
fscom.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package fsos_s3
import (
"bufio"
"fmt"
"github.com/g-portal/switchmgr-go/pkg/config"
"github.com/g-portal/switchmgr-go/pkg/vendors/unimplemented"
"golang.org/x/crypto/ssh"
"golang.org/x/exp/slices"
"io"
"regexp"
"strings"
"time"
)
type FSComS3 struct {
unimplemented.Unimplemented
LoginCommands []string
conn *ssh.Client
session *ssh.Session
writer io.WriteCloser
reader io.Reader
}
// Connect Connecting to a FiberStore switch using SSH
func (fs *FSComS3) Connect(cfg config.Connection) error {
fs.Logger().SetPrefix(fmt.Sprintf("[fscom/%s]", cfg.Host))
var err error
sshConfig := &ssh.ClientConfig{
User: cfg.Username,
Auth: []ssh.AuthMethod{
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
return []string{}, err
}),
},
HostKeyAlgorithms: []string{ssh.KeyAlgoDSA, ssh.KeyAlgoRSA, ssh.KeyAlgoECDSA256, ssh.KeyAlgoED25519},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if cfg.Password != "" {
sshConfig.Auth = append(sshConfig.Auth, ssh.RetryableAuthMethod(ssh.Password(strings.TrimSpace(cfg.Password)), 3))
}
fs.conn, err = ssh.Dial("tcp", fmt.Sprintf("%s:%v", cfg.Host, cfg.Port), sshConfig)
if err != nil {
return err
}
if fs.session, err = fs.conn.NewSession(); err != nil {
return err
}
// Set up terminal modes
fs.Logger().Debugf("Requesting pseudo terminal.")
if err = fs.session.RequestPty("xterm", 5000, 5000, ssh.TerminalModes{}); err != nil {
return fmt.Errorf("request for pseudo terminal failed: %s", err)
}
// create pipe to the stdin of the SSH process
fs.writer, err = fs.session.StdinPipe()
if err != nil {
return fmt.Errorf("failed to create stdin pipe: %s", err)
}
// create pipe to the stdout of the SSH process
fs.reader, err = fs.session.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to create stdout pipe: %s", err)
}
// start the shell
fs.Logger().Debugf("Starting the session.")
if err = fs.session.Shell(); err != nil {
return fmt.Errorf("failed to start the shell: %s", err)
}
// entering the shell
if _, err = fs.SendCommands(fs.LoginCommands...); err != nil {
return err
}
return nil
}
func (fs *FSComS3) Disconnect() error {
if fs.session != nil {
if err := fs.session.Close(); err != nil && err != io.EOF {
return err
}
}
fs.Logger().Debug("Closed the session.")
if fs.conn != nil {
if err := fs.conn.Close(); err != nil && err != io.EOF {
return err
}
}
fs.Logger().Debug("Closed the connection.")
return nil
}
// / save saves the configuration to startup config
func (fs *FSComS3) Save() error {
output, err := fs.SendCommands("write")
if err != nil {
return err
}
if !slices.Contains([]string{"Saving current configuration...\r\n\nOK!", "Building configuration...\r\n\n[OK]"}, strings.TrimSpace(output)) {
return fmt.Errorf("failed to save the configuration: %s", output)
}
return err
}
// SendCommands sends a command to the switch and returns the output
func (fs *FSComS3) SendCommands(commands ...string) (string, error) {
output := ""
startTime := time.Now()
fs.Logger().Debugf("SendCommands %q", commands)
defer func() {
fs.Logger().Debugf("SendCommands %q took %s", commands, time.Since(startTime).String())
}()
reader := bufio.NewReader(fs.reader)
for _, s := range commands {
// send the command to the switch
if _, err := fmt.Fprintf(fs.writer, "%s\n\n", s); err != nil {
return "", fmt.Errorf("failed to send command %q: %s", s, err)
}
// read the output of the command
commandOutput, err := readUntil(s, reader, fs.writer)
if err != nil {
return "", err
}
fs.Logger().Debugf("cmd: %q output: %q", s, commandOutput)
output += commandOutput
}
return output, nil
}
var moreRgx = regexp.MustCompile(`--More--[\s\\b]+`)
func readUntil(command string, reader *bufio.Reader, writer io.Writer) (string, error) {
output := ""
for {
line, err := reader.ReadString('\n')
if err != nil {
if strings.TrimSpace(line) == "" {
break
}
continue
}
// sending enter when switch asks for more even if terminal length is set to 0
if moreRgx.MatchString(line) {
if _, err := fmt.Fprintf(writer, "\n"); err != nil {
return "", fmt.Errorf("failed to send space: %s", err)
}
line = moreRgx.ReplaceAllString(line, "")
}
if commandFinished(line) {
break
}
if !strings.HasSuffix(line, strings.TrimSpace(command)+"\r\n") && line != "" {
output += fmt.Sprintf("%s\n", line)
}
}
return output, nil
}
func commandFinished(line string) bool {
line = strings.TrimSpace(line)
return strings.HasSuffix(line, "#") || strings.HasSuffix(line, ">")
}