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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#!/usr/bin/python
"""
XmConsole.py - Interact with a xen console, getting return codes and
output from commands executed there.
Copyright (C) International Business Machines Corp., 2005
Author: Dan Smith <danms@us.ibm.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; under version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
NB: This requires the domU's prompt to be set to
a _very_ specific value, set in the PROMPT
variable of this script
"""
import sys
import os
import pty
import tty
import termios
import fcntl
import select
import arch
from Test import *
TIMEDOUT = 1
RUNAWAY = 2
class ConsoleError(Exception):
def __init__(self, msg, reason=TIMEDOUT):
self.errMsg = msg
self.reason = reason
def __str__(self):
return str(self.errMsg)
class XmConsole:
def __init__(self, domain, historyLimit=256, historySaveAll=True, historySaveCmds=False, cLimit=131072):
"""
Parameters:
historyLimit: specifies how many lines of history are maintained
historySaveAll: determines whether or not extra messages in
between commands are saved
historySaveCmds : determines whether or not the command echos
are included in the history buffer
"""
self.TIMEOUT = 30
self.PROMPT = "@%@%> "
self.domain = domain
self.historyBuffer = []
self.historyLines = 0
self.historyLimit = historyLimit
self.historySaveAll = historySaveAll
self.historySaveCmds = historySaveCmds
self.debugMe = False
self.limit = cLimit
consoleCmd = ["/usr/sbin/xm", "xm", "console", domain]
if verbose:
print "Console executing: %s" % str(consoleCmd)
pid, fd = pty.fork()
if pid == 0:
os.execvp("/usr/sbin/xm", consoleCmd[1:])
self.consolePid = pid
self.consoleFd = fd
tty.setraw(self.consoleFd, termios.TCSANOW)
def __addToHistory(self, line):
self.historyBuffer.append(line)
self.historyLines += 1
if self.historyLines > self.historyLimit:
self.historyBuffer = self.historyBuffer[1:]
self.historyLines -= 1
def clearHistory(self):
"""Clear the history buffer"""
self.historyBuffer = []
self.historyLines = 0
def getHistory(self):
"""Returns a string containing the entire history buffer"""
output = ""
for line in self.historyBuffer:
output += line + "\n"
return output
def setTimeout(self, timeout):
"""Sets the timeout used to determine if a remote command
has blocked"""
self.TIMEOUT = timeout
def setPrompt(self, prompt):
"""Sets the string key used to delimit the end of command
output"""
self.PROMPT = prompt
def __getprompt(self, fd):
timeout = 0
bytes = 0
buffer = ""
while timeout < 180:
# eat anything while total bytes less than limit else raise RUNAWAY
while (not self.limit) or (bytes < self.limit):
i, o, e = select.select([fd], [], [], 1)
if fd in i:
try:
foo = os.read(fd, 1)
if self.debugMe:
sys.stdout.write(foo)
bytes += 1
buffer += foo
except Exception, exn:
raise ConsoleError(str(exn))
else:
break
else:
raise ConsoleError("Console run-away (exceeded %i bytes)"
% self.limit, RUNAWAY)
# Check to see if the buffer contains anything interetsing
arch.checkBuffer(buffer)
# press enter
os.write(self.consoleFd, "\n")
# look for prompt
for prompt_char in "\r\n" + self.PROMPT:
i, o, e = select.select([fd], [], [], 1)
if fd in i:
try:
foo = os.read(fd, 1)
if self.debugMe:
sys.stdout.write(foo)
if foo != prompt_char:
break
except Exception, exn:
raise ConsoleError(str(exn))
else:
timeout += 1
break
else:
break
else:
raise ConsoleError("Timed out waiting for console prompt")
def __runCmd(self, command, saveHistory=True):
output = ""
line = ""
lines = 0
bytes = 0
self.__getprompt(self.consoleFd)
if verbose:
print "[%s] Sending `%s'" % (self.domain, command)
os.write(self.consoleFd, "%s\n" % command)
while True:
i, o, e = select.select([self.consoleFd], [], [], self.TIMEOUT)
if self.consoleFd in i:
try:
str = os.read(self.consoleFd, 1)
if self.debugMe:
sys.stdout.write(str)
bytes += 1
except Exception, exn:
raise ConsoleError(
"Failed to read from console (fd=%i): %s" %
(self.consoleFd, exn))
else:
raise ConsoleError("Timed out waiting for console command")
if self.limit and bytes >= self.limit:
raise ConsoleError("Console run-away (exceeded %i bytes)"
% self.limit, RUNAWAY)
if str == "\n":
if lines > 0:
output += line + "\n"
if saveHistory:
self.__addToHistory(line)
elif self.historySaveCmds and saveHistory:
self.__addToHistory("*" + line)
lines += 1
line = ""
elif str == "\r":
pass # ignore \r's
else:
line += str
if line == self.PROMPT:
break
return output
def runCmd(self, command):
"""Runs a command on the remote terminal and returns the output
as well as the return code. For example:
ret = c.runCmd("ls")
print ret["output"]
sys.exit(run["return"])
"""
# Allow exceptions to bubble up
realOutput = self.__runCmd(command)
retOutput = self.__runCmd("echo $?", saveHistory=False)
try:
retCode = int(retOutput)
except:
retCode = 255
return {
"output": realOutput,
"return": retCode,
}
def sendInput(self, input):
"""Sends input to the remote terminal, but doesn't check
for a return code"""
realOutput = self.__runCmd(input)
return {
"output": realOutput,
"return": 0,
}
def __closeConsole(self):
"""Closes the console connection and ensures that the console
process is killed. This should only be called by the domain.
Tests should call domain.closeConsole()"""
if self.consolePid != 0:
os.close(self.consoleFd)
os.kill(self.consolePid, 2)
self.consolePid = 0
def setLimit(self, limit):
"""Sets a limit on the number of bytes that can be
read in an attempt to run a command. We need this when
running something that can run away"""
try:
self.limit = int(limit)
except Exception, e:
self.limit = None
def setHistorySaveCmds(self, value):
# True or False
self.historySaveCmds = value
if __name__ == "__main__":
"""This is both an example of using the XmConsole class, as
well as a utility for command-line execution of single commands
on a domU console. Prints output to stdout. Exits with the same
code as the domU command.
"""
verbose = True
try:
t = XmConsole(sys.argv[1])
except ConsoleError, e:
print "Failed to attach to console (%s)" % str(e)
sys.exit(255)
try:
run = t.runCmd(sys.argv[2])
except ConsoleError, e:
print "Console failed (%)" % str(e)
sys.exit(255)
t._XmConsole__closeConsole()
print run["output"],
sys.exit(run["return"])
|