Forcing rsync to non-interactive mode
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
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.
Here are the command line options for ssh that I use to keep it quiet.
ssh -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o batchmode=yes -o passwordauthentication=no
You only need the hostkey stuff if you do not maintain your known_hosts file and are worried about getting MitM warnings. Rather than specifying the authentication types as suggested by James F, I had to explicitly restrict password authentication. I use this to hit hundreds of hosts with a few different OS versions, though, so it may just be an incompatibility.