I am trying to use ssh command in the subprocess.call function of Python 2.6.8 on Unix environment to call a remote script, and I need to provide a string parameter (with whitespaces) to the remote script. However, the string parameter is not being interpreted correctly when I run the python script.

Here is my python script :-


 from subprocess import call


print ret


print ret2

I am getting following output with error and returncode 127 for the ssh command with "whitespace string", and the second ssh command is running fine with returncode 0 :-

bash: /remote/path/to/script/test.sh 'Hi There': No such file or directory




The same shell command executes successfully when I run it on shell command line terminal :-


ssh user@host "/remote/path/to/script/test.sh 'Hi There'";echo $?


Hi There


Here is the content of remote script /remote/path/to/script/test.sh (if it helps) :-


echo $1;

You only need to quote the string that is replacing the working version with Hello, namely '"Hi There"':

ret = call(['ssh','user@host','/remote/path/to/script/test.sh','"Hi There"'])
| improve this answer | |
  • It works.. I am so used to escaping special characters on shell script, that I did not even think about removing the backslashes.. Thanks!! – geek Apr 26 '18 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.