-
Notifications
You must be signed in to change notification settings - Fork 278
/
ssh.go
40 lines (33 loc) · 840 Bytes
/
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
package executables
import (
"context"
"fmt"
)
// SSH is an executable for running SSH commands.
type SSH struct {
Executable
}
const (
sshPath = "ssh"
strictHostCheckFlag = "StrictHostKeyChecking=no"
)
// NewSSH returns a new instance of SSH client.
func NewSSH(executable Executable) *SSH {
return &SSH{
Executable: executable,
}
}
// RunCommand runs a command on the host using SSH.
func (s *SSH) RunCommand(ctx context.Context, privateKeyPath, username, IP string, command ...string) (string, error) {
params := []string{
"-i", privateKeyPath,
"-o", strictHostCheckFlag,
fmt.Sprintf("%s@%s", username, IP),
}
params = append(params, command...)
out, err := s.Executable.Execute(ctx, params...)
if err != nil {
return "", fmt.Errorf("running SSH command: %v", err)
}
return out.String(), nil
}