質問

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.

役に立ちましたか?

解決

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.

他のヒント

Having the same problem, I googled this issue extensively. It would seem you simply cannot pass arguments to ssh with subprocess. Ultimately, I wrote a shell script to run the rsync command, which I could pass arguments to via subprocess.call(['rsyncscript', src, dest, sshkey]). The shell script was: /usr/bin/rsync -az -e "ssh -i $3" $1 $2

This fixed the problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top