-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgluassh.go
230 lines (194 loc) · 4.33 KB
/
gluassh.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gluassh
import (
"bytes"
"fmt"
"io"
"log"
lua "github.com/yuin/gopher-lua"
"golang.org/x/crypto/ssh"
)
func Loader(L *lua.LState) int {
tb := L.NewTable()
L.SetFuncs(tb, map[string]lua.LGFunction{
"connectAndCommand": connectAndCommand,
"login": login,
"sendCommand": sendCommand,
})
L.Push(tb)
return 1
}
func handleError(err error, L *lua.LState) int {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
type LuaSshSess struct {
*ssh.Session
stdin io.WriteCloser
stdout io.Reader
stderr io.Reader
}
// Starts a new ssh session
func login(L *lua.LState) int {
username := L.CheckString(1)
password := L.CheckString(2)
hostname := L.CheckString(3)
port := L.CheckString(4)
secure := L.CheckBool(5)
config := &ssh.ClientConfig{}
if secure {
// SSH client config
config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
// Non-production only
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
} else {
// SSH client config
config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
}
// Connect to host
client, err := ssh.Dial("tcp", hostname+":"+port, config)
if err != nil {
return handleError(err, L)
}
sess, err := client.NewSession()
if err != nil {
return handleError(err, L)
}
stdBuf, err := sess.StdoutPipe()
if err != nil {
return handleError(err, L)
}
stdin, err := sess.StdinPipe()
if err != nil {
return handleError(err, L)
}
stderr, err := sess.StderrPipe()
if err != nil {
return handleError(err, L)
}
err = sess.Shell()
if err != nil {
return handleError(err, L)
}
ud := L.NewUserData()
ud.Value = &LuaSshSess{Session: sess, stdin: stdin, stdout: stdBuf, stderr: stderr}
L.SetMetatable(ud, L.GetTypeMetatable("ssh_session_ud"))
L.Push(ud)
L.Push(lua.LString(string(readConnection(stdBuf))))
return 2
}
func readConnection(stdBuf io.Reader) []byte {
buf := make([]byte, 0, 4096) // big buffer
tmp := make([]byte, 1024) // using small tmo buffer for demonstrating
for {
n, err := stdBuf.Read(tmp)
if err != nil {
if err != io.EOF {
log.Fatal("read error:", err)
}
break
}
buf = append(buf, tmp[:n]...)
if 1024 > n {
break
}
}
return buf
}
// Sends a command to a active ssh session and returns the output
func sendCommand(L *lua.LState) int {
ud := L.CheckUserData(1)
command := L.CheckString(2)
v, _ := ud.Value.(*LuaSshSess)
_, err := fmt.Fprintf(v.stdin, "%s\n", command)
if err != nil {
return handleError(err, L)
}
r := string(readConnection(v.stdout))
L.Push(lua.LString(r))
L.Push(lua.LNil)
return 2
// L.Push(lua.LNil)
// L.Push(lua.LString("Expected ssh_session_ud"))
// return 2
}
// Connects to a host, runs a single command and exits.
// Returns: A string with the output of the command and nil if everything worked. If not it returns
// nil and an error message
func connectAndCommand(L *lua.LState) int {
username := L.CheckString(1)
password := L.CheckString(2)
hostname := L.CheckString(3)
port := L.CheckString(4)
secure := L.CheckBool(5)
command := L.CheckString(6)
config := &ssh.ClientConfig{}
if secure {
// SSH client config
config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
// Non-production only
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
} else {
// SSH client config
config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
}
// Connect to host
client, err := ssh.Dial("tcp", hostname+":"+port, config)
if err != nil {
handleError(err, L)
}
defer client.Close()
// Create sesssion
sess, err := client.NewSession()
if err != nil {
handleError(err, L)
}
defer sess.Close()
// StdinPipe for commands
stdin, err := sess.StdinPipe()
if err != nil {
handleError(err, L)
}
// Uncomment to store output in variable
var b bytes.Buffer
sess.Stdout = &b
sess.Stderr = &b
// Start remote shell
err = sess.Shell()
if err != nil {
handleError(err, L)
}
_, err = fmt.Fprintf(stdin, "%s\n", command)
if err != nil {
handleError(err, L)
}
fmt.Fprintf(stdin, "%s\n", "exit")
// Wait for sess to finish
err = sess.Wait()
if err != nil {
handleError(err, L)
}
L.Push(lua.LString(b.String()))
L.Push(lua.LNil)
return 2
}