Forcing rsync to non-interactive mode

rsyncshell

I would like to use rsync within a python script. I'm calling it using the subprocess module, and authenticating using public keys stored at the authorized_key file on the remote machine.

The only problem is that when I use rsync using a wrong remote user name, I get prompted for password, which obviously halts the backup script forever.

Can I force rsync to exit with error if it can't authenticate, rather than prompting for password?

Udi

Best Answer

Assuming you use rsync with an SSH remote shell (and not - for example - with an rsync server), then you can get rsync to run SSH in a way that will never ask for a password. For example, once can use this call:

rsync -e 'ssh -o "NumberOfPasswordPrompts 0"' source user@target:/path

This will force rsync to use SSH with 0 possible password tries - if its can't gain access using some other authentication method (like public key or GSSAPI) then it will fail with an error. Do note that rsync will not like you when that happens and will complain loudly to STDERR and break with exit code 255.