/usr/lib/python2.7/dist-packages/txwinrm/shell.py is in python-txwinrm 1.1.28-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | ##############################################################################
#
# Copyright (C) Zenoss, Inc. 2013, all rights reserved.
#
# This content is made available according to terms specified in the LICENSE
# file at the top-level directory of this package.
#
##############################################################################
import re
import logging
import shlex
import base64
import csv
from itertools import izip
from pprint import pformat
from cStringIO import StringIO
from twisted.internet import reactor, defer, task
from twisted.internet.error import TimeoutError
from xml.etree import cElementTree as ET
from . import constants as c
from .util import create_etree_request_sender, get_datetime
log = logging.getLogger('winrm')
_MAX_REQUESTS_PER_COMMAND = 9999
class CommandResponse(object):
def __init__(self, stdout, stderr, exit_code):
self._stdout = stdout
self._stderr = stderr
self._exit_code = exit_code
@property
def stdout(self):
return self._stdout
@property
def stderr(self):
return self._stderr
@property
def exit_code(self):
return self._exit_code
def __repr__(self):
return pformat(dict(
stdout=self.stdout, stderr=self.stderr, exit_code=self.exit_code))
def _build_command_line_elem(command_line):
command_line_parts = shlex.split(command_line, posix=False)
prefix = "rsp"
ET.register_namespace(prefix, c.XML_NS_MSRSP)
command_line_elem = ET.Element('{%s}CommandLine' % c.XML_NS_MSRSP)
command_elem = ET.Element('{%s}Command' % c.XML_NS_MSRSP)
command_elem.text = command_line_parts[0]
command_line_elem.append(command_elem)
for arguments_text in command_line_parts[1:]:
arguments_elem = ET.Element('{%s}Arguments' % c.XML_NS_MSRSP)
arguments_elem.text = arguments_text
command_line_elem.append(arguments_elem)
tree = ET.ElementTree(command_line_elem)
str_io = StringIO()
tree.write(str_io)
return str_io.getvalue()
def _stripped_lines(stream_parts):
results = []
for line in ''.join(stream_parts).splitlines():
if line.strip():
results.append(line.strip())
return results
def _find_shell_id(elem):
xpath = './/{%s}Selector[@Name="ShellId"]' % c.XML_NS_WS_MAN
return elem.findtext(xpath).strip()
def _find_command_id(elem):
xpath = './/{%s}CommandId' % c.XML_NS_MSRSP
return elem.findtext(xpath).strip()
def _find_stream(elem, command_id, stream_name):
xpath = './/{%s}Stream[@Name="%s"][@CommandId="%s"]' \
% (c.XML_NS_MSRSP, stream_name, command_id)
for elem in elem.findall(xpath):
if elem.text is not None:
yield base64.decodestring(elem.text)
def _find_exit_code(elem, command_id):
command_state_xpath = './/{%s}CommandState[@CommandId="%s"]' \
% (c.XML_NS_MSRSP, command_id)
command_state_elem = elem.find(command_state_xpath)
if command_state_elem is not None:
exit_code_xpath = './/{%s}ExitCode' % c.XML_NS_MSRSP
exit_code_text = command_state_elem.findtext(exit_code_xpath)
return None if exit_code_text is None else int(exit_code_text)
class SingleShotCommand(object):
def __init__(self, sender):
self._sender = sender
@defer.inlineCallbacks
def run_command(self, command_line):
"""
Run commands in a remote shell like the winrs application on Windows.
Accepts multiple commands. Returns a dictionary with the following
structure:
CommandResponse
.stdout = [<non-empty, stripped line>, ...]
.stderr = [<non-empty, stripped line>, ...]
.exit_code = <int>
"""
shell_id = yield self._create_shell()
try:
cmd_response = yield self._run_command(shell_id, command_line)
except TimeoutError:
yield self._sender.close_connections()
yield self._delete_shell(shell_id)
yield self._sender.close_connections()
defer.returnValue(cmd_response)
@defer.inlineCallbacks
def _create_shell(self):
elem = yield self._sender.send_request('create')
defer.returnValue(_find_shell_id(elem))
@defer.inlineCallbacks
def _run_command(self, shell_id, command_line):
command_line_elem = _build_command_line_elem(command_line)
command_elem = yield self._sender.send_request(
'command', shell_id=shell_id, command_line_elem=command_line_elem,
timeout=self._sender._sender._conn_info.timeout)
command_id = _find_command_id(command_elem)
stdout_parts = []
stderr_parts = []
for i in xrange(_MAX_REQUESTS_PER_COMMAND):
receive_elem = yield self._sender.send_request(
'receive', shell_id=shell_id, command_id=command_id)
stdout_parts.extend(
_find_stream(receive_elem, command_id, 'stdout'))
stderr_parts.extend(
_find_stream(receive_elem, command_id, 'stderr'))
exit_code = _find_exit_code(receive_elem, command_id)
if exit_code is not None:
break
else:
raise Exception("Reached max requests per command.")
yield self._sender.send_request(
'signal',
shell_id=shell_id,
command_id=command_id,
signal_code=c.SHELL_SIGNAL_TERMINATE)
stdout = _stripped_lines(stdout_parts)
stderr = _stripped_lines(stderr_parts)
defer.returnValue(CommandResponse(stdout, stderr, exit_code))
@defer.inlineCallbacks
def _delete_shell(self, shell_id):
yield self._sender.send_request('delete', shell_id=shell_id)
def create_single_shot_command(conn_info):
sender = create_etree_request_sender(conn_info)
return SingleShotCommand(sender)
class LongRunningCommand(object):
def __init__(self, sender):
self._sender = sender
self._shell_id = None
self._command_id = None
self._exit_code = None
@defer.inlineCallbacks
def start(self, command_line):
log.debug("LongRunningCommand run_command: {0}".format(command_line))
elem = yield self._sender.send_request('create')
self._shell_id = _find_shell_id(elem)
command_line_elem = _build_command_line_elem(command_line)
log.debug('LongRunningCommand run_command: sending command request '
'(shell_id={0}, command_line_elem={1})'.format(
self._shell_id, command_line_elem))
try:
command_elem = yield self._sender.send_request(
'command', shell_id=self._shell_id,
command_line_elem=command_line_elem,
timeout=self._sender._sender._conn_info.timeout)
except TimeoutError:
yield self._sender.close_connections()
raise
self._command_id = _find_command_id(command_elem)
@defer.inlineCallbacks
def receive(self):
try:
receive_elem = yield self._sender.send_request(
'receive',
shell_id=self._shell_id,
command_id=self._command_id)
except TimeoutError:
yield self._sender.close_connections()
raise
stdout_parts = _find_stream(receive_elem, self._command_id, 'stdout')
stderr_parts = _find_stream(receive_elem, self._command_id, 'stderr')
self._exit_code = _find_exit_code(receive_elem, self._command_id)
stdout = _stripped_lines(stdout_parts)
stderr = _stripped_lines(stderr_parts)
defer.returnValue((stdout, stderr))
@defer.inlineCallbacks
def stop(self):
yield self._sender.send_request(
'signal',
shell_id=self._shell_id,
command_id=self._command_id,
signal_code=c.SHELL_SIGNAL_CTRL_C)
try:
stdout, stderr = yield self.receive()
except TimeoutError:
# close_connections done in receive()
pass
yield self._sender.send_request(
'signal',
shell_id=self._shell_id,
command_id=self._command_id,
signal_code=c.SHELL_SIGNAL_TERMINATE)
yield self._sender.send_request('delete', shell_id=self._shell_id)
defer.returnValue(CommandResponse(stdout, stderr, self._exit_code))
def create_long_running_command(conn_info):
sender = create_etree_request_sender(conn_info)
return LongRunningCommand(sender)
@defer.inlineCallbacks
def create_long_running_shell(conn_info):
results = {}
sender = create_etree_request_sender(conn_info)
elem = yield sender.send_request('create')
shell_id = _find_shell_id(elem)
results['sender'] = sender
results['shell_id'] = shell_id
defer.returnValue(results)
@defer.inlineCallbacks
def retrieve_long_running_shell(sender, shell_id, command_line):
stdout_parts = []
stderr_parts = []
exit_code = None
command_line_elem = _build_command_line_elem(command_line)
command_elem = yield sender.send_request(
'command', shell_id=shell_id,
command_line_elem=command_line_elem)
command_id = _find_command_id(command_elem)
for i in xrange(3):
receive_elem = yield sender.send_request(
'receive',
shell_id=shell_id,
command_id=command_id)
stdout_parts.extend(
_find_stream(receive_elem, command_id, 'stdout'))
stderr_parts.extend(
_find_stream(receive_elem, command_id, 'stderr'))
exit_code = _find_exit_code(receive_elem, command_id)
stdout = _stripped_lines(stdout_parts)
stderr = _stripped_lines(stderr_parts)
defer.returnValue(CommandResponse(stdout, stderr, exit_code))
class Typeperf(object):
def __init__(self, long_running_command):
self._long_running_command = long_running_command
self._counters = None
self._row_count = 0
@defer.inlineCallbacks
def start(self, counters, time_between_samples=1):
self._counters = counters
self._row_count = 0
quoted_counters = ['"{0}"'.format(c) for c in counters]
command_line = 'typeperf {0} -si {1}'.format(
' '.join(quoted_counters), time_between_samples)
yield self._long_running_command.start(command_line)
@defer.inlineCallbacks
def receive(self):
"""
Returns a pair, (<dictionary>, <stripped stderr lines>), where the
dictionary is {<counter>: [(<datetime>, <float>)]}"""
stdout, stderr = yield self._long_running_command.receive()
dct = {}
for counter in self._counters:
dct[counter] = []
for row in csv.reader(stdout):
self._row_count += 1
if self._row_count == 1:
continue
try:
timestamp = get_datetime(row[0])
except ValueError as e:
log.debug('Typeperf receive {0}. {1}'.format(row, e))
continue
for counter, value in izip(self._counters, row[1:]):
dct[counter].append((timestamp, float(value)))
defer.returnValue((dct, stderr))
@defer.inlineCallbacks
def stop(self):
yield self._long_running_command.stop()
self._counters = None
self._row_count = 0
def create_typeperf(conn_info):
long_running_command = create_long_running_command(conn_info)
return Typeperf(long_running_command)
class RemoteShell(object):
_PROMPT_PATTERN = re.compile(r'[A-Z]:\\.*>$')
_READ_DELAY = 0.2
def __init__(self, sender, include_exit_codes=False):
self._sender = sender
self._include_exit_codes = include_exit_codes
self._reset()
def __del__(self):
self.delete()
@property
def prompt(self):
return self._prompt
@defer.inlineCallbacks
def create(self):
if self._shell_id is not None:
self.delete()
log.debug("RemoteShell create: sending create request")
elem = yield self._sender.send_request('create')
self._shell_id = _find_shell_id(elem)
command_line_elem = _build_command_line_elem('cmd')
log.debug('RemoteShell create: sending command request (shell_id={0}, '
'command_line_elem={1})'.format(
self._shell_id, command_line_elem))
command_elem = yield self._sender.send_request(
'command', shell_id=self._shell_id,
command_line_elem=command_line_elem,
timeout=self._sender._sender._conn_info.timeout)
self._command_id = _find_command_id(command_elem)
self._deferred_receiving = self._start_receiving()
stdout = []
stderr = []
while self._prompt is None:
out, err = yield task.deferLater(
reactor, self._READ_DELAY, self._get_output)
stderr.extend(err)
for line in out:
if self._PROMPT_PATTERN.match(line):
self._prompt = line
else:
stdout.append(line)
defer.returnValue(CommandResponse(stdout, stderr, None))
@defer.inlineCallbacks
def run_command(self, command):
stdout, stderr = yield self._run_command(command)
if self._include_exit_codes:
o2, e2 = yield self._run_command('echo %errorlevel%')
exit_code = o2[0]
else:
exit_code = None
defer.returnValue(CommandResponse(stdout, stderr, exit_code))
@defer.inlineCallbacks
def delete(self):
if self._shell_id is None:
return
self.run_command('exit')
exit_code = yield self._deferred_receiving
yield self._sender.send_request(
'signal',
shell_id=self._shell_id,
command_id=self._command_id,
signal_code=c.SHELL_SIGNAL_TERMINATE)
yield self._sender.send_request('delete', shell_id=self._shell_id)
stdout, stderr = self._get_output()
self._reset()
defer.returnValue(CommandResponse(stdout, stderr, exit_code))
def _reset(self):
self._shell_id = None
self._command_id = None
self._deferred_receiving = None
self._prompt = None
self._stdout_parts = []
self._stderr_parts = []
@defer.inlineCallbacks
def _start_receiving(self):
exit_code = None
while exit_code is None:
receive_elem = yield task.deferLater(
reactor, self._READ_DELAY, self._sender.send_request,
'receive', shell_id=self._shell_id,
command_id=self._command_id)
self._stdout_parts.extend(
_find_stream(receive_elem, self._command_id, 'stdout'))
self._stderr_parts.extend(
_find_stream(receive_elem, self._command_id, 'stderr'))
exit_code = _find_exit_code(receive_elem, self._command_id)
defer.returnValue(exit_code)
def _get_output(self):
stdout = _stripped_lines(self._stdout_parts)
stderr = _stripped_lines(self._stderr_parts)
del self._stdout_parts[:]
del self._stderr_parts[:]
return stdout, stderr
@defer.inlineCallbacks
def _run_command(self, command):
base64_encoded_command = base64.encodestring('{0}\r\n'.format(command))
yield self._sender.send_request(
'send',
shell_id=self._shell_id,
command_id=self._command_id,
base64_encoded_command=base64_encoded_command)
stdout = []
stderr = []
for i in xrange(_MAX_REQUESTS_PER_COMMAND):
out, err = yield task.deferLater(
reactor, self._READ_DELAY, self._get_output)
stderr.extend(err)
if not out:
continue
stdout.extend(out[:-1])
if out[-1] == self._prompt:
break
stdout.append(out[-1])
else:
raise Exception("Reached max requests per command.")
defer.returnValue((stdout, stderr))
def create_remote_shell(conn_info, include_exit_codes=False):
sender = create_etree_request_sender(conn_info)
return RemoteShell(sender, include_exit_codes)
|