/
machine_ssh_session.go
83 lines (69 loc) · 1.73 KB
/
machine_ssh_session.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
package util
import (
"bytes"
"fmt"
"sync"
"golang.org/x/crypto/ssh"
)
// Memory area content reading and writing
type safeBuffer struct {
buffer bytes.Buffer
mu sync.Mutex
}
func (w *safeBuffer) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.buffer.Write(p)
}
func (w *safeBuffer) Bytes() []byte {
w.mu.Lock()
defer w.mu.Unlock()
return w.buffer.Bytes()
}
func (w *safeBuffer) Reset() {
w.mu.Lock()
defer w.mu.Unlock()
w.buffer.Reset()
}
func (w *safeBuffer) String() string {
w.mu.Lock()
defer w.mu.Unlock()
return w.buffer.String()
}
// LogicSSHSession struct
type LogicSSHSession struct {
stdOut *safeBuffer // ssh session Stdout
stdErr *safeBuffer // ssh session StdErr
session *ssh.Session
}
// CloseSSHSession function
func (s *LogicSSHSession) CloseSSHSession() {
if s.session != nil {
s.session.Close()
}
}
// createLogicSSHSession function,the according ssh.Client create ssh session shell
func createLogicSSHSession(sshClient *ssh.Client) (*LogicSSHSession, error) {
sshSession, err := sshClient.NewSession()
if err != nil {
return nil,
fmt.Errorf("SSH client new session failed: %v", err.Error())
}
stdOutWriter := new(safeBuffer)
stdErrWriter := new(safeBuffer)
sshSession.Stdout = stdOutWriter
sshSession.Stderr = stdErrWriter
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := sshSession.RequestPty("xterm", 80, 40, modes); err != nil {
return nil, fmt.Errorf("SSH session requestPty failed: %v", err.Error())
}
return &LogicSSHSession{
session: sshSession,
stdOut: stdOutWriter,
stdErr: stdErrWriter,
}, nil
}