/
ssh.go
149 lines (126 loc) · 3.58 KB
/
ssh.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
package ssh
import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type SSHClient struct {
remoteAddr string
username string
sshSigner ssh.Signer
}
func NewSSHClient(remoteAddr string, username string, sshKeyFile string) (*SSHClient, error) {
if strings.HasPrefix(sshKeyFile, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
sshKeyFile = strings.Replace(sshKeyFile, "~", homeDir, 1)
}
sshKeyData, err := os.ReadFile(sshKeyFile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(sshKeyData)
if err != nil {
return nil, err
}
s := &SSHClient{
remoteAddr: remoteAddr,
username: username,
sshSigner: signer,
}
return s, nil
}
func (s *SSHClient) RemoveAddr() string {
return s.remoteAddr
}
// AuthorizedKey returns the public key in authorized_keys format
func (s *SSHClient) AuthorizedKey() string {
return string(ssh.MarshalAuthorizedKey(s.sshSigner.PublicKey()))
}
// Exec executes a command and returns stdout or not nil error
func (s *SSHClient) Exec(ctx context.Context, cmd string) (string, error) {
stdout, stderr, exitCode, err := s.ExecVerbose(ctx, cmd)
if err != nil || exitCode != 0 {
return "", fmt.Errorf("command %s exit %d stderr %s", cmd, exitCode, stderr)
}
return stdout, nil
}
// Run runs commands in sequence, if any command fails, it will return error
func (s *SSHClient) Run(ctx context.Context, cmds ...string) error {
for _, cmd := range cmds {
stdout, stderr, exitCode, err := s.ExecVerbose(ctx, cmd)
if err != nil {
return fmt.Errorf("command %s error: %w", cmd, err)
}
if exitCode != 0 {
return fmt.Errorf("command %s exit %d, stdout %s, stderr %s", cmd, exitCode, stdout, stderr)
}
}
return nil
}
// ExecVerbose executes a command and returns stdout, stderr, exitCode or not nil error
func (s *SSHClient) ExecVerbose(_ context.Context, cmd string) (stdout string, stderr string, exitCode int, err error) {
sshConn, err := s.newConn()
if err != nil {
return "", "", -1, err
}
sess, err := sshConn.NewSession()
if err != nil {
return "", "", -1, err
}
stdoutBuf := bytes.NewBuffer(nil)
stderrBuf := bytes.NewBuffer(nil)
sess.Stdout, sess.Stderr = stdoutBuf, stderrBuf
cmd = fmt.Sprintf(`bash --login -c "%s"`, cmd)
err = sess.Start(cmd)
if err != nil {
return "", "", -1, err
}
err = sess.Wait()
if err != nil {
if exitErr, ok := err.(*ssh.ExitError); ok {
exitCode = exitErr.ExitStatus()
} else {
return "", "", -1, err
}
}
return stdoutBuf.String(), stderrBuf.String(), exitCode, nil
}
func (s *SSHClient) newConn() (*ssh.Client, error) {
return ssh.Dial("tcp", s.remoteAddr, &ssh.ClientConfig{
User: s.username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(s.sshSigner),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
}
func (s *SSHClient) WriteFile(content io.Reader, dstFilePath string) error {
sshConn, err := s.newConn()
if err != nil {
return fmt.Errorf("%s@%s failed to create ssh connection: %w", s.username, s.remoteAddr, err)
}
defer sshConn.Close()
sftpClient, err := sftp.NewClient(sshConn)
if err != nil {
return fmt.Errorf("%s@%s failed to create sftp client: %w", s.username, s.remoteAddr, err)
}
defer sftpClient.Close()
file, err := sftpClient.Create(dstFilePath)
if err != nil {
return fmt.Errorf("%s@%s failed to create file %s: %w", s.username, s.remoteAddr, dstFilePath, err)
}
defer file.Close()
_, err = file.ReadFrom(content)
if err != nil {
return fmt.Errorf("%s@%s failed to write file %s: %w", s.username, s.remoteAddr, dstFilePath, err)
}
return nil
}