0

I'm running a small code that uses fabric's rsync_project function to connect to a remote machine and transfer a file to it. I've assigned env.password with the password of the server.

However, when I run the code, I get prompted for password. After entering the password, the file gets transferred. But I do not want to get prompted.

Here's my code:

from fabric import environment
from fabric.contrib.project import rsync_project
env.hosts = ['172.16.154.134']
env.password = 'user@123'
def sync(remote_dir, local_dir):
    rsync_project(remote_dir, local_dir)

Maybe I have misunderstood what env.password is for. If so, please tell me some other way to get rid of the prompt that asks for password.

Thanks

Aamir Khan
  • 324
  • 2
  • 10
  • Relevant github issue: https://github.com/fabric/fabric/issues/817 – Sinkingpoint May 19 '17 at 10:40
  • @Sinkingpoint The link throws a 404. It's not working. – Aamir Khan May 19 '17 at 10:42
  • have a look at this so question http://stackoverflow.com/q/3737003/5476782 – Evhz May 19 '17 at 10:54
  • @Karlos This link provides my question as the answer. The point is, even after setting env.password, I'm being prompted for a password. – Aamir Khan May 19 '17 at 11:02
  • the first answer tells you to use shell=False, did you try that? – Evhz May 19 '17 at 11:04
  • @Karlos By configuring if you mean generating a key pair, I can do that. But I don't want to do that. Reason is that I'll connect to multiple systems and putting the public key file on each one of them will not be possible. – Aamir Khan May 19 '17 at 11:05
  • then in fabric you can try `env.passwords` as it says [here](https://github.com/fabric/fabric/issues/976) and check fabric docs for **the version of fabric** you use – Evhz May 19 '17 at 11:10
  • @Karlos Downgraded fabric to 1.6.0. Tried with both env.password and env.passwords. Still not working. – Aamir Khan May 19 '17 at 11:25

1 Answers1

1

This is a common misunderstanding. Once you set env in code, whatever function you call should be called with execute.

from fabric.state import env
from fabric.decorators import task
from fabric.api import execute
from fabric.contrib.project import rsync_project

env.hosts = ['172.16.154.134']
env.password = 'user@123'

def internal_sync(remote_dir, local_dir):
    rsync_project(remote_dir, local_dir)

# use task to define the external endpoint for the fab command line
@task
def sync(remote_dir, local_dir):
    """Does an rsync from remote_dir => local_dir"""
    # above docstring for `fab --list`
    # call `internal_sync` using the env
    # parameters we set in code
    execute(internal_sync, remote_dir, local_dir)

And from the command line:

fab sync:/path/to/remote,/path/to/local
2ps
  • 15,099
  • 2
  • 27
  • 47