Skip to content

Commit f60b077

Browse files
author
Dan
committed
Changed hardcoded bash to user login defined shell. Added multiple single quote test
1 parent debdbde commit f60b077

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pssh/ssh_client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/test_pssh_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,16 @@ def test_ssh_exception(self):
601601
raise Exception("Expected SSHException")
602602
server.kill()
603603
del _socket
604+
605+
def test_multiple_single_quotes_in_cmd(self):
606+
output = self.client.run_command("echo 'me' 'and me'")
607+
self.client.join(output)
608+
stdout = list(output[self.host]['stdout'])
609+
expected = 'me and me'
610+
self.assertTrue(output[self.host]['exit_code'] == 0,
611+
msg="Error executing cmd with multiple single quotes - %s" % (
612+
stdout,))
613+
self.assertEqual([expected], stdout,
614+
msg="Got unexpected output. Expected %s, got %s" % (
615+
expected, stdout,))
616+

0 commit comments

Comments
 (0)