supplying password to subprocess.call('rsync ...'), os.system('rsync ...')

David wizzardx at gmail.com
Sun Oct 7 04:43:32 EDT 2007


On 10/5/07, timw.google <tjandacw at yahoo.com> wrote:
> Hi
>
> I want to write a python script that runs rsync on a given directory
> and host. I build the command line string, but when I try to run
> subprocess.call(cmd), or p=subprocess.Popen(cmd, shell=True),or
> os.system(cmd), I get prompted for my login password. I expected this,
> but when I try to give my password, it's echoed back to the terminal
> and the special characters in the password is (I think) getting
> interpreted by the shell (zsh)
>
> I can't ssh w/o supplying a password. That's the way the security is
> set up here.
>
> How do I use python to do this, or do I just have to write a zsh
> script?
>

SSH takes measures to ensure that passwords are typed from a keyboard
(pty) rather than being piped in automatically (stdin).

If SSH public key authentication (see ssh-keygen) doesn't work for
you, then try ssh agent (see, ssh-add), "sshpass", or something like
"empty-expect".

If you use ssh public keys, you can also setup the public key so that
only a specified command can be run, and that the public key can only
be used from a specific host. This is more secure. See this page for
more info: http://troy.jdmz.net/rsync/index.html

Otherwise you may need to do some pty-hackery in python to fool ssh
into thinking it's password is being entered from a keyboard and not a
script. However, you should try public key authentication (with rsync
as the only allowed command) first.

Another method is to setup an ssh service on the server (perhaps in
inetd). One disadvantage of this is that the rsync session (including
rsync login passwords) is not encrypted.



More information about the Python-list mailing list