Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fabric keeps asking for password using SSH connection

I'm trying to connect to a windows azure instance using fabric, but despite I configure ssh conection to execute commands, fabric keeps asking for password.

This is my fabric file:

def azure1():
    env.hosts = ['host.cloudapp.net:60770']
    env.user = 'adminuser'
    env.key_filename = './azure.key'

def what_is_my_name():
    run('whoami')

I run it as:

fab -f fabfile.py azure1  what_is_my_name

or

fab -k -f fabfile.py -i azure.key -H [email protected]:60770 -p password what_is_my_name

But nothing worked, it keeps asking for user password despite I enter it correctly.

Executing task 'what_is_my_name'
run: whoami
Login password for 'adminuser': 
Login password for 'adminuser': 
Login password for 'adminuser': 
Login password for 'adminuser': 

If I try to connect directly with ssh, it works perfectly.

ssh -i azure.key -p 60770 [email protected]

I've tried the advises given in other questions (q1 q2 q3) but nothing works.

Any idea what I am doing wrong?

Thank you

like image 674
kothvandir Avatar asked Mar 29 '13 17:03

kothvandir


1 Answers

Finally I found the problem is due to the public-private key pair generation.

I followed the steps provided in windows azure guide, there the keys are generated using openssl, so the process outcomes a public key stored in a pem file you must upload to your instance during creation process.

The problem is that this private key obtained is not correctly recognized by paramiko, so fabric won't work. If you try to open a ssh connection using paramiko from python interpreter:

>>> import paramiko, os
>>> paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
>>> ssh = paramiko.SSHClient()
>>> ssh.load_host_keys('private_key_file.key') # private key file generated using openssl
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect("web1.cloudapp.net",port=56317)

Gives me the error:

DEBUG:paramiko.transport:Trying SSH agent key a9d8dd41609191ebeedbe8df768ad8c9
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".. /paramiko/client.py", line 337, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
  File ".. /paramiko/client.py", line 528, in _auth
    raise saved_exception
paramiko.PasswordRequiredException: Private key file is encrypted

When the key file isn't encrypted.

To solve this, I created the key pair using openssh and then convert the public key to pem to upload it to azure:

# Create key with openssh
ssh-keygen -t rsa -b 2048 -f private_key_file.key

# extract public key and store as x.509 pem format
openssl req -x509 -days 365 -new -key private_key_file.key -out public_key_file.pem

# upload public_key_file.pem file during instance creation

# check connection to instance
ssh -i private_key_file.key -p 63534 [email protected] 

This solved the problem.

like image 166
kothvandir Avatar answered Sep 22 '22 16:09

kothvandir