pythonrsyncpopenrsh

python popen rsync with rsh option


I'm trying to execute a rsync command via subrocess & popen. Everything's ok until I don't put the rsh subcommand where things go wrong.

from subprocess import Popen
args = ['-avz', '--rsh="ssh -C -p 22 -i /home/bond/.ssh/test"', 'bond@localhost:/home/bond/Bureau', '/home/bond/data/user/bond/backups/']

p = Popen(['rsync'] + args, shell=False)
print p.wait()

#just printing generated command:
print ' '.join(['rsync']+args)

I've tried to escape the '--rsh="ssh -C -p 22 -i /home/bond/.ssh/test"' in many ways, but it seems that it's not the problem.

I'm getting the error rsync: Failed to exec ssh -C -p 22 -i /home/bond/.ssh/test: No such file or directory (2)

If I copy/paste the same args that I output at the time, I'm getting a correct execution of the command.

Thanks.


Solution

  • What happens if you use '--rsh=ssh -C -p 22 -i /home/bond/.ssh/test' instead (I removed the double quotes).

    I suspect that this should work. What happens when you cut/paste your line into the commandline is that your shell sees the double quotes and removes them but uses them to prevent -C -p etc. from being interpreted as separate arguments. when you call subprocess.Popen with a list, you've already partitioned the arguments without the help of the shell, so you no longer need the quotes to preserve where the arguments should be split.