This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
/
ssh.go
122 lines (97 loc) · 2.42 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
package ssh // import "cirello.io/gochatbot/plugins/gochatbot-plugin-ops/ssh"
import (
"fmt"
"io/ioutil"
"net"
"strings"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type sshCommand struct {
command string
env []string
}
type clientSSH struct {
config *ssh.ClientConfig
host string
port string
}
func (client *clientSSH) runCommand(cmd *sshCommand) ([]byte, error) {
var session *ssh.Session
var err error
if session, err = client.newSession(); err != nil {
return []byte{}, err
}
defer session.Close()
if err = client.prepareCommand(session, cmd); err != nil {
return []byte{}, err
}
return session.CombinedOutput(cmd.command)
}
func (client *clientSSH) prepareCommand(session *ssh.Session, cmd *sshCommand) error {
for _, env := range cmd.env {
variable := strings.Split(env, "=")
if len(variable) != 2 {
continue
}
if err := session.Setenv(variable[0], variable[1]); err != nil {
return err
}
}
return nil
}
func (client *clientSSH) newSession() (*ssh.Session, error) {
connection, err := ssh.Dial("tcp", net.JoinHostPort(client.host, client.port), client.config)
if err != nil {
return nil, fmt.Errorf("cannot dial: %s", err)
}
session, err := connection.NewSession()
if err != nil {
return nil, fmt.Errorf("cannot create session: %s", err)
}
modes := ssh.TerminalModes{
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
session.Close()
return nil, fmt.Errorf("cannot request pseudo terminal: %s", err)
}
return session, nil
}
func PublicKeyFile(file string) ssh.AuthMethod {
b, err := ioutil.ReadFile(file)
if err != nil {
return nil
}
key, err := ssh.ParsePrivateKey(b)
if err != nil {
return nil
}
return ssh.PublicKeys(key)
}
func SshAgent(getenv func(string) string) ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
func Run(command string, env []string, username, host, port string, authMethod ssh.AuthMethod) ([]byte, error) {
sshConfig := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{authMethod},
}
client := &clientSSH{
config: sshConfig,
host: host,
port: port,
}
if client.port == "" {
client.port = "22"
}
cmd := &sshCommand{
command: command,
env: env,
}
return client.runCommand(cmd)
}