Skip to content

Commit 3cffa3f

Browse files
author
Dan
committed
Added returning stdin in output. Added flag to turn off use of pty
1 parent a23030c commit 3cffa3f

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pssh/pssh_client.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,22 +516,22 @@ def get_output(self, cmd, output):
516516
0
517517
"""
518518
try:
519-
(channel, host, stdout, stderr) = cmd.get()
519+
(channel, host, stdout, stderr, stdin) = cmd.get()
520520
except Exception, ex:
521521
try:
522522
host = ex.args[1]
523523
except IndexError:
524524
logger.error("Got exception with no host argument - "
525525
"cannot update output data with %s", ex)
526526
raise ex
527-
self._update_host_output(output, host, None, None, None, None, cmd,
527+
self._update_host_output(output, host, None, None, None, None, None, cmd,
528528
exception=ex)
529529
raise ex
530530
self._update_host_output(output, host, self._get_exit_code(channel),
531-
channel, stdout, stderr, cmd)
531+
channel, stdout, stderr, stdin, cmd)
532532

533533
def _update_host_output(self, output, host, exit_code, channel, stdout,
534-
stderr, cmd, exception=None):
534+
stderr, stdin, cmd, exception=None):
535535
"""Update host output with given data"""
536536
if host in output:
537537
new_host = "_".join([host,
@@ -546,6 +546,7 @@ def _update_host_output(self, output, host, exit_code, channel, stdout,
546546
'channel' : channel,
547547
'stdout' : self._read_buff_ex_code(stdout, output),
548548
'stderr' : self._read_buff_ex_code(stderr, output),
549+
'stdin' : stdin,
549550
'cmd' : cmd,
550551
'exception' : exception,})
551552

@@ -615,7 +616,7 @@ def get_stdout(self, greenlet, return_buffers=False):
615616
warnings.warn("This method is being deprecated and will be removed in"
616617
"future releases - use self.get_output instead", DeprecationWarning)
617618
gevent.sleep(.2)
618-
channel, host, stdout, stderr = greenlet.get()
619+
channel, host, stdout, stderr, stdin = greenlet.get()
619620
if channel.exit_status_ready():
620621
channel.close()
621622
else:

pssh/ssh_client.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,30 +202,46 @@ def _connect(self, client, host, port, sock=None, retries=1):
202202

203203
def exec_command(self, command, sudo=False, user=None,
204204
shell=None,
205-
use_shell=True, **kwargs):
205+
use_shell=True, use_pty=True,
206+
**kwargs):
206207
"""Wrapper to :mod:`paramiko.SSHClient.exec_command`
207208
208209
Opens a new SSH session with a new pty and runs command before yielding
209210
the main gevent loop to allow other greenlets to execute.
210211
211-
:param command: Shell command to execute
212+
:param command: Cxommand to execute
212213
:type command: str
213214
:param sudo: (Optional) Run with sudo. Defaults to False
214215
:type sudo: bool
216+
:param user: (Optional) User to switch to via sudo to run command as. \
217+
Defaults to user running the python process
218+
:type user: str
219+
:param shell: (Optional) Shell override to use instead of user login \
220+
configured shell. For example ``shell='bash -c'``
221+
:param use_shell: (Optional) Force use of shell on/off. \
222+
Defaults to `True` for on
223+
:type use_shell: bool
224+
:param use_pty: (Optional) Enable/Disable use of pseudo terminal \
225+
emulation. This is required in vast majority of cases, exception \
226+
being where a shell is not used and stdout/stderr/stdin buffers \
227+
are not required. Defaults to ``True``
228+
:type use_pty: bool
215229
:param kwargs: (Optional) Keyword arguments to be passed to remote \
216230
command
217231
:type kwargs: dict
218-
:rtype: Tuple of `(channel, hostname, stdout, stderr)`. \
232+
:rtype: Tuple of `(channel, hostname, stdout, stderr, stdin)`. \
219233
Channel is the remote SSH channel, needed to ensure all of stdout has \
220234
been got, hostname is remote hostname the copy is to, stdout and \
221235
stderr are buffers containing command output.
222236
"""
223237
channel = self.client.get_transport().open_session()
224238
if self.forward_ssh_agent:
225239
agent_handler = paramiko.agent.AgentRequestHandler(channel)
226-
channel.get_pty()
240+
if use_pty:
241+
channel.get_pty()
227242
if self.channel_timeout:
228243
channel.settimeout(self.channel_timeout)
244+
stdin = channel.makefile('wb')
229245
_stdout, _stderr = channel.makefile('rb'), \
230246
channel.makefile_stderr('rb')
231247
stdout, stderr = self._read_output_buffer(_stdout,), \
@@ -247,7 +263,7 @@ def exec_command(self, command, sudo=False, user=None,
247263
channel.exec_command(_command, **kwargs)
248264
logger.debug("Command started")
249265
sleep(0)
250-
return channel, self.host, stdout, stderr
266+
return channel, self.host, stdout, stderr, stdin
251267

252268
def _read_output_buffer(self, output_buffer, prefix=''):
253269
"""Read from output buffers and log to host_logger"""

0 commit comments

Comments
 (0)