Skip to content

Commit 6997fe6

Browse files
author
Pan
committed
Fix quoting of command when sudo is used on native client. Resolves #112
1 parent c472360 commit 6997fe6

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Change Log
22
============
33

4+
1.5.5
5+
++++++
6+
7+
Fixes
8+
------
9+
10+
* Use of ``sudo`` in native client incorrectly required escaping of command.
11+
412
1.5.4
513
++++++
614

pssh/ssh2_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,8 @@ def run_command(self, command, sudo=False, user=None,
364364
_command = 'sudo -S '
365365
elif user:
366366
_command = 'sudo -u %s -S ' % (user,)
367-
if shell:
368-
_command += '%s "%s"' % (shell, command,)
369-
else:
370-
_command += '$SHELL -c "%s"' % (command,)
367+
_shell = shell if shell else '$SHELL -c'
368+
_command += "%s '%s'" % (_shell, command,)
371369
channel = self.execute(_command, use_pty=use_pty)
372370
return channel, self.host, \
373371
self.read_output_buffer(

tests/test_pssh_ssh2_client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,7 @@ def test_bash_variable_substitution(self):
777777
command = """for i in 1 2 3; do echo $i; done"""
778778
output = list(self.client.run_command(command)[self.host]['stdout'])
779779
expected = ['1','2','3']
780-
self.assertEqual(output, expected,
781-
msg="Unexpected output from bash variable substitution %s - should be %s" % (
782-
output, expected,))
780+
self.assertListEqual(output, expected)
783781

784782
def test_identical_host_output(self):
785783
"""Test that we get output when running with duplicated hosts"""
@@ -1133,6 +1131,13 @@ def test_run_command_sudo(self):
11331131
self.assertTrue(self.host in output)
11341132
self.assertTrue(output[self.host].channel is not None)
11351133

1134+
def test_run_command_sudo_var(self):
1135+
command = """for i in 1 2 3; do echo $i; done"""
1136+
output = list(self.client.run_command(
1137+
command, sudo=True)[self.host]['stdout'])
1138+
expected = ['1','2','3']
1139+
self.assertListEqual(output, expected)
1140+
11361141
def test_conn_failure(self):
11371142
"""Test connection error failure case - ConnectionErrorException"""
11381143
client = ParallelSSHClient(['127.0.0.100'], port=self.port,

0 commit comments

Comments
 (0)