@@ -187,12 +187,13 @@ def _connect(self, client, host, port, sock=None, retries=1):
187187 logger .error (msg )
188188 raise SSHException (msg , host , port )
189189
190- def exec_command (self , command , sudo = False , user = None , ** kwargs ):
190+ def exec_command (self , command , sudo = False , user = None ,
191+ shell = None ,
192+ use_shell = True , ** kwargs ):
191193 """Wrapper to :mod:`paramiko.SSHClient.exec_command`
192194
193- Opens a new SSH session with a new pty and runs command with given \
194- `kwargs` if any. Greenlet then yields (sleeps) while waiting for \
195- command to finish executing or channel to close indicating the same.
195+ Opens a new SSH session with a new pty and runs command before yielding
196+ the main gevent loop to allow other greenlets to execute.
196197
197198 :param command: Shell command to execute
198199 :type command: str
@@ -217,15 +218,19 @@ def exec_command(self, command, sudo=False, user=None, **kwargs):
217218 stdout , stderr = self ._read_output_buffer (_stdout ,), \
218219 self ._read_output_buffer (_stderr ,
219220 prefix = '\t [err]' )
221+
222+ shell = '$SHELL -c' if not shell else shell
223+ _command = ''
220224 if sudo and not user :
221- command = 'sudo -S bash -c \' %s \' ' % ( command ,)
225+ _command = 'sudo -S '
222226 elif user :
223- command = 'sudo -u %s -S bash -c \' %s\' ' % (
224- user , command ,)
227+ _command = 'sudo -u %s -S ' % (user ,)
228+ if use_shell :
229+ _command += '%s \' %s\' ' % (shell , command ,)
225230 else :
226- command = 'bash -c \' %s\' ' % (command ,)
227- logger .debug ("Running command %s on %s" , command , self .host )
228- channel .exec_command (command , ** kwargs )
231+ _command + = '\' %s\' ' % (command ,)
232+ logger .debug ("Running parsed command %s on %s" , _command , self .host )
233+ channel .exec_command (_command , ** kwargs )
229234 logger .debug ("Command started" )
230235 sleep (0 )
231236 return channel , self .host , stdout , stderr
0 commit comments