/
ssh.go
executable file
·104 lines (84 loc) · 2.41 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
package exe
import (
"bytes"
"net"
"os"
"strings"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"github.com/shadracnicholas/home-automation/libraries/go/oops"
)
// RemoteCmd is a command that is executed over SSH
type RemoteCmd struct {
Cmd string
PseudoTTY bool
}
// RequestPseudoTTY will force the command to request a pty on the remote server.
func (c *RemoteCmd) RequestPseudoTTY() *RemoteCmd {
c.PseudoTTY = true
return c
}
// RemoteCommand creates a new remote command. You can specify the whole command
// as a single string, or as separate arguments that get joined by spaces.
func RemoteCommand(name string, args ...string) *RemoteCmd {
elems := append([]string{name}, args...)
return &RemoteCmd{
Cmd: strings.Join(elems, " "),
}
}
// Run executes the command using the given SSH client
func (c *RemoteCmd) Run(client *ssh.Client) Result {
errParams := map[string]string{
"cmd": c.Cmd,
}
session, err := client.NewSession()
if err != nil {
return Result{
Err: oops.WithMessage(err, "failed to start session", errParams),
}
}
defer func() { _ = session.Close() }()
if c.PseudoTTY {
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
}
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
return Result{
Err: oops.WithMessage(err, "failed to request Pty", errParams),
}
}
}
result := Result{}
var stdout, stderr bytes.Buffer
session.Stdout = &stdout
session.Stderr = &stderr
err = session.Run(c.Cmd)
result.Stdout = strings.TrimSpace(stdout.String())
result.Stderr = strings.TrimSpace(stderr.String())
if err != nil {
result.Err = oops.WithMessage(err, result.Stderr, errParams)
}
return result
}
// SSHClient returns an SSH client for the given username@host.
// It assumes ssh-agent will be available for key authentication.
func SSHClient(username, host string) (*ssh.Client, error) {
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
return nil, oops.WithMessage(err, "failed to open SSH_AUTH_SOCK")
}
agentClient := agent.NewClient(conn)
clientConfig := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agentClient.Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", host+":22", clientConfig)
if err != nil {
return nil, oops.WithMessage(err, "failed to dial %s", host)
}
return client, nil
}