0

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 :-

 #!/usr/bin/python

 from subprocess import call

 ret=call(['ssh','user@host','\"/remote/path/to/script/test.sh','\'Hi','There\'\"'])

print ret

ret2=call(['ssh','user@host','/remote/path/to/script/test.sh','Hello'])

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

127

Hello

0

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

Command:

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

Output:

Hi There

0

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

#!/usr/bin/ksh

echo $1;
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"'])
  • It works.. I am so used to escaping special characters on shell script, that I did not even think about removing the backslashes.. Thanks!! – Arjit Dixit 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.