Skip to main content
 首页 » 编程设计

Python Fabric 运行命令返回 "binascii.Error: Incorrect padding"

2025年05月04日10wayfarer

我正在尝试使用下面的脚本通过 fabric 连接到 amazon EC2。但是我遇到了一个我不确定如何解决的问题。

import os 
from fabric.api import run, env, local, cd 
 
WORK = os.getenv('HOME') + '/Work/myproject/' 
 
env.user = 'ubuntu' 
env.hosts = [ 
    '128.248.268.288' 
] 
env.key_filename = [ 
    '%s/aws/myproject.pem' % WORK 
] 
 
def deploy(): 
    print("Executing on %(host)s as %(user)s" % env) 
    with cd('/sites/myproject.com/code/'): 
        run('ls') 

这是回溯。我不确定如何解决这个问题。

Traceback (most recent call last): 
  File "/Library/Python/2.6/site-packages/fabric/main.py", line 540, in main 
    commands[name](*args, **kwargs) 
  File "/Users/mickeyckm/Work/myproject/codes/giivee/fabfile.py", line 18, in deploy 
    run('ls') 
  File "/Library/Python/2.6/site-packages/fabric/network.py", line 391, in host_prompting_wrapper 
    return func(*args, **kwargs) 
  File "/Library/Python/2.6/site-packages/fabric/operations.py", line 422, in run 
    channel = connections[env.host_string]._transport.open_session() 
  File "/Library/Python/2.6/site-packages/fabric/network.py", line 65, in __getitem__ 
    self[real_key] = connect(user, host, port) 
  File "/Library/Python/2.6/site-packages/fabric/network.py", line 140, in connect 
    client.load_system_host_keys() 
  File "/Library/Python/2.6/site-packages/paramiko/client.py", line 151, in load_system_host_keys 
    self._system_host_keys.load(filename) 
  File "/Library/Python/2.6/site-packages/paramiko/hostkeys.py", line 155, in load 
    e = HostKeyEntry.from_line(line) 
  File "/Library/Python/2.6/site-packages/paramiko/hostkeys.py", line 67, in from_line 
    key = RSAKey(data=base64.decodestring(key)) 
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/base64.py", line 321, in decodestring 
    return binascii.a2b_base64(s) 
binascii.Error: Incorrect padding 

任何帮助/提示将不胜感激。

干杯, 米奇

请您参考如下方法:

我看到一些地方的 binascii 模块导致了不正确的填充错误,这主要是因为您传递的字符串包含一些无关的空白字符。

>>> import binascii 
>>> binascii.a2b_base64('a') 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
binascii.Error: Incorrect padding 

在您的例子中,它是您为 env 对象设置的各种属性。对您的关键文件位置执行类似的操作,看看是否可行。

filelocation = os.path.join(WORK,'aws/myproject.pem') 
env.key_filename = [filelocation]