Skip to content

Instantly share code, notes, and snippets.

@smurn
Created July 17, 2014 09:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smurn/4d45a51b3a571fa0d35d to your computer and use it in GitHub Desktop.
Save smurn/4d45a51b3a571fa0d35d to your computer and use it in GitHub Desktop.
Monkey patch for paramiko issue 291
"""
Add binary-mode options to SSHClient.exec_command(...) for binary stdout, stderr channels.
See: https://github.com/paramiko/paramiko/issues/291
"""
import paramiko
def _patched_exec_command(self,
command,
bufsize=-1,
timeout=None,
get_pty=False,
stdin_binary=True,
stdout_binary=False,
stderr_binary=False):
chan = self._transport.open_session()
if get_pty:
chan.get_pty()
chan.settimeout(timeout)
chan.exec_command(command)
stdin = chan.makefile('wb' if stdin_binary else 'w', bufsize)
stdout = chan.makefile('rb' if stdin_binary else 'r', bufsize)
stderr = chan.makefile_stderr('rb' if stdin_binary else 'r', bufsize)
return stdin, stdout, stderr
paramiko.SSHClient.exec_command = _patched_exec_command
@pansapiens
Copy link

Should lines 25 and 26 be using stdout_binary and stderr_binary, respectively, rather than all using the same stdin_binary flag ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment