Siva Arunachalam
Siva Arunachalam

Reputation: 7750

Using passwords in fabric execute command

I was trying to pull the logs from one of our servers using fabric. This needs to be executed from a different python program. The problem is we don't have SSH keys and we have only passwords. Could you please help me to provide passwords to connect to the server?

Code:

from fabric.api import *
from fabric.tasks import execute

def get_logs():
    get('/var/logs/custom', ".")

if __name__ == '__main__':
    execute(get_logs, hosts=["username@hostname",])

Error

**[username@hostname] Executing task 'get_logs'
Traceback (most recent call last):
  File "D:\Python\ws\fabric\Fabric_test\example.py", line 8, in <module>
    execute(get_logs, hosts=["username@hostname",])
  File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 368, in execute
    multiprocessing
  File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 264, in _execute
    return task.run(*args, **kwargs)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 171, in run
    return self.wrapped(*args, **kwargs)
  File "D:\Python\ws\fabric\Fabric_test\example.py", line 5, in get_logs
    get('/var/logs/custom/', ".")
  File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 639, in host_prompting_wrapper
    return func(*args, **kwargs)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\operations.py", line 528, in get
    ftp = SFTP(env.host_string)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\sftp.py", line 30, in __init__
    self.ftp = connections[host_string].open_sftp()
  File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 151, in __getitem__
    self.connect(key)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 143, in connect
    self[key] = connect(user, host, port, cache=self)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 523, in connect
    password = prompt_for_password(text)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 604, in prompt_for_password
    new_password = _password_prompt(password_prompt, stream)
  File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 576, in _password_prompt
    return getpass.getpass(prompt.encode('ascii', 'ignore'), stream)
TypeError: pydev_getpass() takes at most 1 argument (2 given)

**

Upvotes: 2

Views: 669

Answers (2)

Siva Arunachalam
Siva Arunachalam

Reputation: 7750

Finally solved by executing as a OS Shell Command. Here is the entire snippet.

import os, shutil
from subprocess import check_output

from fabric.api import get

def get_logs(source_dir):
    get('/var/logs/custom', source_dir)

def fetch_logs(hostname, username, password):
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    current_file_name = os.path.abspath(__file__).replace(".pyc", ".py")
    local_path = os.path.join(BASE_DIR, 'data', hostname)
    try:
        shutil.rmtree(local_path)
    except:
        pass
    exe = os.path.join(BASE_DIR, "..", "Scripts", "fab.exe")
    input_cmd = exe + " -H " + hostname + " -u " + username + " -p " + password + " -f " + current_file_name + " get_logs:source_dir:" + local_path
    try:
        check_output(input_cmd, shell=True)
    except Exception as e:
        print e
        return False
    shutil.move(os.path.join(BASE_DIR, 'download'), local_path)
    return True

Upvotes: 0

jfs
jfs

Reputation: 414675

It looks like you are running the script from IDE (PyDev) that replaces getpass.getpass() function by its own function pydev_getpass() that has different signature (it doesn't accept the second argument stream).

If you run the script from the command line; you should not get this error.

Upvotes: 1

Related Questions