Mickey Cheong
Mickey Cheong

Reputation: 3010

Python Fabric run command returns "binascii.Error: Incorrect padding"

I'm trying to connect to connect to amazon EC2 via fabric using the script below. But I'm met with a problem that I'm not sure how to solve it.

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')

This is the traceback. I'm not sure how to solve the problem.

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

Any help/hint would be great appreciated.

Cheers, Mickey

Upvotes: 1

Views: 5640

Answers (3)

rolandw
rolandw

Reputation: 45

I had a similar problem and tracked it down to some corruption in my .ssh/known_hosts file.

I thus added to my .bashrc

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

(obviously putting the right fabric script where <myfabscript> is) and now all works fine when I simply run "deploy"!

Upvotes: 0

Robert Kajic
Robert Kajic

Reputation: 9077

Look at your ~/.ssh/known_hosts file. It may contain lines with duplicate entries or be corrupted in some other way.

Upvotes: 5

Senthil Kumaran
Senthil Kumaran

Reputation: 56941

I saw some places where Incorrect Padding error was resulted from binascii module and it was mostly when the string you pass has some extraneous white-space characters.

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

In your case, it the various properties you set for your env object. Do something like this for your key file location and see if that works.

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

Upvotes: 5

Related Questions