Skip to main content
 首页 » 编程设计

python之在 fabric execute 命令中使用密码

2025年05月04日9kenshinobiy

我试图使用结构从我们的一台服务器中提取日志。这需要从不同的 python 程序执行。问题是我们没有 SSH key ,我们只有密码。你能帮我提供连接到服务器的密码吗?

代码:

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",]) 

错误

**[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) 

**

请您参考如下方法:

看起来您正在从 IDE ( PyDev ) 运行替换 getpass.getpass() 的脚本通过自身的功能发挥作用 pydev_getpass()具有不同的签名(它不接受第二个参数 stream )。

如果您从命令行运行脚本;你不应该得到这个错误。