Python Fabric运行命令返回“binascii.Error:不正确的填充”

时间:2011-02-21 08:38:53

标签: python fabric

我正在尝试连接以使用下面的脚本通过结构连接到亚马逊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

非常感谢任何帮助/提示。

干杯, 米奇

3 个答案:

答案 0 :(得分:5)

我看到了一些地方,其中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]

答案 1 :(得分:5)

查看〜/ .ssh / known_hosts文件。它可能包含具有重复条目的行或以其他方式被破坏。

答案 2 :(得分:0)

我遇到了类似的问题,并将其追溯到我的.ssh / known_hosts文件中的一些损坏。

我因此加入了我的.bashrc

alias deploy='mv ~/.ssh/known_hosts ~/.ssh/known_hosts.tmp; fab <myfabscript>; mv ~/.ssh/known_hosts.old ~/.ssh/known_hosts'

(显然将正确的结构脚本放在<myfabscript>所在的位置),现在只需运行“deploy”就可以了!