forked from crsgyj/executor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
176 lines (154 loc) · 3.39 KB
/
session.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package executor
import (
"bytes"
"fmt"
"runtime"
"time"
"unicode"
//"os"
"strings"
//"github.com/spf13/cobra/cobra/cmd"
"net"
"os/exec"
"golang.org/x/crypto/ssh"
)
type execSessionBuilder = func() (execSession, error)
type execSession interface {
Run(cmd string, env []string) error
Close() error
Output() string
ErrMsg() string
}
type SessionConfig struct {
User string
Password string
Host string
Port int8
Timeout int
}
type localSession struct {
cmd *exec.Cmd
msgBuf *bytes.Buffer
errBuf *bytes.Buffer
}
type remoteSession struct {
session *ssh.Session
msgBuf *bytes.Buffer
errBuf *bytes.Buffer
}
func newRemoteSession(config SessionConfig) execSessionBuilder {
return func() (execSession, error) {
r := remoteSession{
msgBuf: &bytes.Buffer{},
errBuf: &bytes.Buffer{},
}
session, err := sshConnect(config.Host, config.Port, config.User, config.Password, config.Timeout)
if err != nil {
return nil, err
}
r.session = session
r.session.Stdout = r.msgBuf
r.session.Stderr = r.errBuf
return r, nil
}
}
func newLocalSession() execSessionBuilder {
return func() (execSession, error) {
r := localSession{
cmd: &exec.Cmd{},
msgBuf: &bytes.Buffer{},
errBuf: &bytes.Buffer{},
}
r.cmd.Stdout = r.msgBuf
r.cmd.Stderr = r.errBuf
return r, nil
}
}
func (r localSession) Run(command string, env []string) error {
args := strings.FieldsFunc(command, unicode.IsSpace)
path, _ := exec.LookPath(args[0])
r.cmd.Path = path
r.cmd.Args = args
if env != nil {
r.cmd.Env = env
}
if err := r.cmd.Run(); err != nil {
r.errBuf.WriteString(err.Error())
return err
}
return nil
}
func (r localSession) Close() error {
return nil
}
func (r localSession) Output() string {
return r.msgBuf.String()
}
func (r localSession) ErrMsg() string {
return r.errBuf.String()
}
func (r remoteSession) Run(command string, env []string) error {
envSets := getEnvSets(env)
err := r.session.Run(envSets + command)
if err != nil {
r.errBuf.WriteString(err.Error())
}
return err
}
func (r remoteSession) Close() error {
return r.session.Close()
}
func (r remoteSession) Output() string {
return r.msgBuf.String()
}
func (r remoteSession) ErrMsg() string {
return r.errBuf.String()
}
func sshConnect(host string, port int8, user, password string, timeout int) (*ssh.Session, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
client *ssh.Client
session *ssh.Session
err error
)
if timeout == 0 {
timeout = 20000
}
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(password))
hostKeyCb := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}
clientConfig = &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: time.Duration(timeout) * time.Second,
HostKeyCallback: hostKeyCb,
}
addr = fmt.Sprintf("%s:%d", host, port)
if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
// create session
if session, err = client.NewSession(); err != nil {
return nil, err
}
return session, nil
}
/// 获取env输出语句
func getEnvSets(env []string) string {
if env == nil && len(env) == 0 {
return ""
}
str := ""
if runtime.GOOS == "windows" {
str += "set "
} else {
str += "export "
}
str += strings.Join(env, " ") + " && "
return str
}