-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
56 lines (45 loc) · 1.45 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
package sharedaction
import "code.cloudfoundry.org/cli/util/clissh"
type TTYOption clissh.TTYRequest
const (
RequestTTYAuto TTYOption = iota
RequestTTYNo
RequestTTYYes
RequestTTYForce
)
type LocalPortForward clissh.LocalPortForward
type SSHOptions struct {
Commands []string
Username string
Passcode string
Endpoint string
HostKeyFingerprint string
SkipHostValidation bool
SkipRemoteExecution bool
TTYOption TTYOption
LocalPortForwardSpecs []LocalPortForward
}
func (actor Actor) ExecuteSecureShell(sshClient SecureShellClient, sshOptions SSHOptions) error {
err := sshClient.Connect(sshOptions.Username, sshOptions.Passcode, sshOptions.Endpoint, sshOptions.HostKeyFingerprint, sshOptions.SkipHostValidation)
if err != nil {
return err
}
defer sshClient.Close()
err = sshClient.LocalPortForward(convertActorToSSHPackageForwardingSpecs(sshOptions.LocalPortForwardSpecs))
if err != nil {
return err
}
if sshOptions.SkipRemoteExecution {
err = sshClient.Wait()
} else {
err = sshClient.InteractiveSession(sshOptions.Commands, clissh.TTYRequest(sshOptions.TTYOption))
}
return err
}
func convertActorToSSHPackageForwardingSpecs(actorSpecs []LocalPortForward) []clissh.LocalPortForward {
sshPackageSpecs := []clissh.LocalPortForward{}
for _, spec := range actorSpecs {
sshPackageSpecs = append(sshPackageSpecs, clissh.LocalPortForward(spec))
}
return sshPackageSpecs
}