Skip to content

Commit

Permalink
Added running as different user than logged in user - resolves #13
Browse files Browse the repository at this point in the history
  • Loading branch information
pkittenis committed Nov 4, 2014
1 parent 60a9b1b commit 79e4458
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
See :mod:`pssh.ParallelSSHClient` and :mod:`pssh.SSHClient` for class documentation.
"""

import logging
import gevent.pool
from gevent import socket
from gevent import monkey
monkey.patch_all()
import logging
import paramiko
import os

Expand Down Expand Up @@ -168,7 +168,7 @@ def _connect(self, retries=1):
logger.error("Error executing ProxyCommand - %s", e.message,)
raise ProxyCommandException(e.message)

def exec_command(self, command, sudo=False, **kwargs):
def exec_command(self, command, sudo=False, user=None, **kwargs):
"""Wrapper to :mod:`paramiko.SSHClient.exec_command`
Opens a new SSH session with a pty and runs command with given \
Expand Down Expand Up @@ -196,8 +196,11 @@ def exec_command(self, command, sudo=False, **kwargs):
channel.makefile_stderr('rb'))
stdout, stderr = self._read_output_buffer(_stdout), \
self._read_output_buffer(_stderr)
if sudo:
if sudo and not user:
command = 'sudo -S bash -c "%s"' % command.replace('"', '\\"')
elif user:
command = 'sudo -u %s -S bash -c "%s"' % (
user, command.replace('"', '\\"'),)
else:
command = 'bash -c "%s"' % command.replace('"', '\\"')
logger.debug("Running command %s on %s", command, self.host)
Expand All @@ -211,7 +214,7 @@ def _read_output_buffer(self, output_buffer):
"""Read from output buffers,
allowing coroutines to execute in between reading"""
for line in output_buffer:
gevent.sleep(1)
gevent.sleep()
yield line.strip()

def _make_sftp(self):
Expand Down Expand Up @@ -469,7 +472,7 @@ def wait_for_exit_status(self, channel):
WARNING - this will block forever if the command executed never exits
:rtype: int - Exit code of command executed"""
while not channel.exit_status_ready():
gevent.sleep(1)
gevent.sleep()
channel.close()
return channel.recv_exit_status()

Expand Down

0 comments on commit 79e4458

Please sign in to comment.