-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
143 lines (126 loc) · 2.72 KB
/
client.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
package ssh
import (
"bytes"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
func NewSshClient() *Client {
return &Client{}
}
func NewSshClientKey(username string, rsaKeyPair RsaKeyPair, host string) *Client {
return &Client{
username: username,
password: "",
rsaKeyPair: &rsaKeyPair,
host: host,
}
}
type Client struct {
username string
password string
rsaKeyPair *RsaKeyPair
host string
}
func (p *Client) SetConnectionData(username string, password string, host string) {
p.username = username
p.password = password
p.host = host
}
func (p *Client) Executef(command string, a ...any) (ExecutionResult, error) {
return p.Execute(fmt.Sprintf(command, a...))
}
func (p *Client) Execute(command string) (ExecutionResult, error) {
var config *ssh.ClientConfig
if p.rsaKeyPair == nil {
config = &ssh.ClientConfig{
User: p.username,
Auth: []ssh.AuthMethod{
ssh.Password(p.password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
} else {
// create signer
signer, err := ssh.ParsePrivateKey(p.rsaKeyPair.PrivateKey)
if err != nil {
return ExecutionResult{}, err
}
config = &ssh.ClientConfig{
User: p.username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}
// connect to ssh server
conn, err := ssh.Dial("tcp", p.host+":22", config)
if err != nil {
return ExecutionResult{}, err
}
// Session
session, err := conn.NewSession()
if err != nil {
return ExecutionResult{}, err
}
defer func() {
err := session.Close()
if err != nil {
log.WithError(err).Error("cannot close session")
}
err = conn.Close()
if err != nil {
log.WithError(err).Error("cannot close connection")
}
}()
//Run command
var buffOut bytes.Buffer
var buffErr bytes.Buffer
session.Stdout = &buffOut
session.Stderr = &buffErr
err = session.Run(command)
if err != nil {
errOut := buffErr.String()
if len(errOut) == 0 {
errOut = buffOut.String()
}
return ExecutionResult{
Output: buffOut.String(),
error: &ExecutionError{
Output: errOut,
Code: err.(*ssh.ExitError).ExitStatus(),
},
}, nil
}
return ExecutionResult{
Output: buffOut.String(),
}, nil
}
type ExecutionError struct {
Output string
Code int
}
func (e *ExecutionError) ToError() error {
return errors.New(fmt.Sprintf("[%d] - %s", e.Code, e.Output))
}
type ExecutionResult struct {
Output string
error *ExecutionError
}
func (r ExecutionResult) Error() error {
if r.error != nil {
return r.error.ToError()
}
return nil
}
func (r ExecutionResult) Code() int {
if r.error != nil {
return r.error.Code
}
return 0
}
func (r ExecutionResult) IsError() bool {
return r.error != nil
}