-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_expect.go
255 lines (203 loc) · 5.93 KB
/
cmd_expect.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"bufio"
"context"
"encoding/csv"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
"github.com/skx/subcommands"
expect "github.com/google/goexpect"
)
// Structure for our options and state.
type expectCommand struct {
// Timeout for running our command/waiting.
//
// This is set via the script-file, rather than a command-line argument.
timeout time.Duration
// We embed the NoFlags option, because we accept no command-line flags.
subcommands.NoFlags
}
// Info returns the name of this subcommand.
func (ec *expectCommand) Info() (string, string) {
return "expect", `A simple utility for scripting interactive commands.
Details:
This command allows you to execute an arbitrary process, sending input for
matching output which is received. It is a simple alternative to the 'expect'
utility, famously provided with TCL.
The command requires a configuration file to be specified which contains details
of the process to be executed, and the output/input to receive/send.
Here is a simple example, note that the output the command produces is matched via regular expressions, rather than literally. That's why you'll see "\." used to match a literal period:
# Comments are prefixed with '#'
# Timeout is expressed in seconds
TIMEOUT 10
# The command to run
SPAWN telnet telehack.com
# Now the dialog
EXPECT \n\.
SEND date\r\n
EXPECT \n\.
SEND quit\r\n
You'll see we use '\r\n' because we're using telnet, for driving bash and other normal commands you'd use '\n' instead as you would expect:
TIMEOUT 10
SPAWN /bin/bash --login
EXPECT $
SEND touch /tmp/meow\n
EXPECT $
SEND exit\n
If you wish to execute a command, or arguments, containing spaces that is supported via quoting:
SPAWN /path/to/foo arg1 "argument two" arg3 ..
`
}
// Run a command, and return something suitable for matching against with
// the expect library we're using..
func (ec *expectCommand) expectExec(cmd []string) (*expect.GExpect, func() error, error) {
c := exec.CommandContext(
context.Background(),
cmd[0], cmd[1:]...)
// write error out to my stdout
c.Stderr = os.Stderr
stdIn, err := c.StdinPipe()
if err != nil {
return nil, nil, fmt.Errorf("error creating pipe: %s", err)
}
stdOut, err := c.StdoutPipe()
if err != nil {
return nil, nil, fmt.Errorf("error creating pipe: %s", err)
}
if err = c.Start(); err != nil {
return nil, nil, fmt.Errorf("unexpected error starting command: %+v", err)
}
waitCh := make(chan error, 1)
e, _, err := expect.SpawnGeneric(
&expect.GenOptions{
In: stdIn,
Out: stdOut,
Wait: func() error {
er := c.Wait()
waitCh <- er
return err
},
Close: c.Process.Kill,
Check: func() bool {
if c.Process == nil {
return false
}
return c.Process.Signal(syscall.Signal(0)) == nil
},
},
ec.timeout,
expect.Verbose(true),
expect.VerboseWriter(os.Stdout),
)
if err != nil {
return nil, nil, fmt.Errorf("error creating expect: %s", err)
}
wait := func() error {
err := <-waitCh
return err
}
return e, wait, nil
}
// Execute is invoked if the user specifies `expect` as the subcommand.
func (ec *expectCommand) Execute(args []string) int {
// Ensure we have a config-file
if len(args) <= 0 {
fmt.Printf("Usage: expect /path/to/config.script\n")
return 1
}
// We'll now open the configuration file
handle, err := os.Open(args[0])
if err != nil {
fmt.Printf("error opening %s : %s\n", args[0], err.Error())
return 1
}
// Timeout Value
ec.timeout = 60 * time.Second
// Command
cmd := "/bin/sh"
// Read/Send stuff
interaction := []expect.Batcher{}
// Allow reading line by line
reader := bufio.NewReader(handle)
line, err := reader.ReadString(byte('\n'))
for err == nil {
// Lose the space
line = strings.TrimSpace(line)
// Timeout?
if strings.HasPrefix(line, "TIMEOUT ") {
line = strings.TrimPrefix(line, "TIMEOUT ")
line = strings.TrimSpace(line)
val, er := strconv.Atoi(line)
if er != nil {
fmt.Printf("error converting timeout value %s to number: %s\n", line, er)
return 1
}
ec.timeout = time.Duration(val) * time.Second
}
// Command
if strings.HasPrefix(line, "SPAWN ") {
cmd = strings.TrimPrefix(line, "SPAWN ")
cmd = strings.TrimSpace(cmd)
}
// Expect
if strings.HasPrefix(line, "EXPECT ") {
line = strings.TrimPrefix(line, "EXPECT ")
line = strings.TrimSpace(line)
line = strings.ReplaceAll(line, "\\n", "\n")
line = strings.ReplaceAll(line, "\\r", "\r")
line = strings.ReplaceAll(line, "\\t", "\t")
interaction = append(interaction, &expect.BExp{R: line})
}
// Send
if strings.HasPrefix(line, "SEND ") {
line = strings.TrimPrefix(line, "SEND ")
line = strings.TrimSpace(line)
line = strings.ReplaceAll(line, "\\n", "\n")
line = strings.ReplaceAll(line, "\\r", "\r")
line = strings.ReplaceAll(line, "\\t", "\t")
interaction = append(interaction, &expect.BSnd{S: line})
}
// Loop again
line, err = reader.ReadString(byte('\n'))
}
// Launch the command
fmt.Printf("Running: '%s'\n", cmd)
// Split the command into fields, taking into account quoted strings.
//
// So the user can run things like this:
// echo "foo bar" 3
//
// https://stackoverflow.com/questions/47489745/
//
r := csv.NewReader(strings.NewReader(cmd))
r.Comma = ' '
record, err := r.Read()
if err != nil {
fmt.Printf("failed to split %s : %s\n", cmd, err)
return 1
}
// Launch the command using the record array we've just parsed.
e, wait, err := ec.expectExec(record)
if err != nil {
fmt.Printf("error launching %s: %s\n", cmd, err)
return 1
}
defer e.Close()
// Wire up the expect-magic.
_, err = e.ExpectBatch(interaction, ec.timeout)
if err != nil {
fmt.Printf("error running recipe:%s\n", err)
return 1
}
// Now await completion of the command/process.
if err := wait(); err != nil {
fmt.Printf("error waiting for process: %s\n", err)
return 1
}
return 0
}