Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions winrm/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,34 @@ def cleanup_command(self, shell_id, command_id):
# TODO change assert into user-friendly exception
assert uuid.UUID(relates_to.replace('uuid:', '')) == message_id

def send_command_input(self, shell_id, command_id, input):
"""
Send input to the given shell and command.
@param string shell_id: The shell id on the remote machine.
See #open_shell
@param string command_id: The command id on the remote machine.
See #run_command
@param string input: The input unicode string to be sent.
@return: None
Comment thread
rustyscottweber marked this conversation as resolved.
"""
req = {'env:Envelope': self._get_soap_header(
resource_uri='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd', # NOQA
action='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send', # NOQA
shell_id=shell_id)}
stdin = req['env:Envelope'].setdefault('env:Body', {}).setdefault(
'rsp:Send', {}).setdefault('rsp:Stream', {})
stdin['@CommandId'] = command_id
stdin['@Name'] = 'stdin'
stdin['@xmlns:rsp'] = 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell'
# Always sending "\\r\\n" when sending input is not always what a user wants..
# if a want a new line.. Add it yourself. Not every one wants or needs the new line.
# input = (input + '\r\n').encode('ascii')
Comment thread
rustyscottweber marked this conversation as resolved.
Outdated
stdin['#text'] = base64.b64encode(input)
#print(xmltodict.unparse(req))
res = self.send_message(xmltodict.unparse(req))
#print(res)
return res

def get_command_output(self, shell_id, command_id):
"""
Get the Output of the given shell and command
Expand Down
14 changes: 14 additions & 0 deletions winrm/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ def test_get_command_output(protocol_fake):
protocol_fake.close_shell(shell_id)


def test_send_command_input(protocol_fake):
shell_id = protocol_fake.open_shell()
command_id = protocol_fake.run_command(shell_id, 'cmd')
protocol_fake.send_command_input('echo "hello world"')
std_out, std_err, status_code = protocol_fake.get_command_output(
shell_id, command_id)
assert status_code == 0
assert b'hello world' in std_out
assert len(std_err) == 0

protocol_fake.cleanup_command(shell_id, command_id)
protocol_fake.close_shell(shell_id)


def test_set_timeout_as_sec():
protocol = Protocol('endpoint',
username='username',
Expand Down