1

I found a lot of similar questions here in SO, but any of them helped me, even this problem seems to be simple.

I'm trying to connect to a remote Google Compute Engine instance through SSH (I want to use SFTP) using paramiko. Below is my code:

        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.connect('External_IP_Get_In_GCE_panel', username='myuser')
        stdin, stdout, stderr = client.exec_command('ls')
        for line in stdout:
            print('... ' + line.strip('\n'))
        client.close()

With this code, I have the error

PasswordRequiredException: Private key file is encrypted

When I tried client.connect('External_IP_Get_In_GCE_panel', username='myuser', password='') the error is:

BadAuthenticationType: ('Bad authentication type', [u'publickey']) (allowed_types=[u'publickey'])

My SSH key to access Google Compute engine has no password. I can use the gcloud compute ssh instance-name and also access SFTP throug Filezilla without problems.

As I said, I tried a lot of alternatives found here in SO, but any of them helped me. Below are 3 other versions of the code:

Using key

    key = paramiko.RSAKey(data=base64.b64decode(b"""AAAAB3Nza..."""))
    client = paramiko.SSHClient()
    client.get_host_keys().add('External_IP_Get_In_GCE_panel', 'ssh-rsa', key)
    client.connect('External_IP_Get_In_GCE_panel', username='myuser', password='')
    # I got the key using ssh-keyscan `External_IP_Get_In_GCE_panel`in my local machine

Using another lib

    key = paramiko.RSAKey(data=decodebytes(keydata))
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys.add('External_IP_Get_In_GCE_panel', 'ssh-rsa', key)

    with pysftp.Connection('External_IP_Get_In_GCE_panel', username='myuser', cnopts=cnopts) as sftp:
        with sftp.cd('../directory'):
            sftp.get('remote_file')'''

Using the ssh key file

    self.client = paramiko.SSHClient()
    self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.client.connect(hostname="External_IP_Get_In_GCE_panel", username="myuser", key_filename=os.path.expanduser('~/.ssh/ssh_key_file'), password='') # also tried ssh_key_file.pub

In all this versions (and some others) I tried to use password='', password=None and not send password argument. The results is always the same erros above.

Any tip about what I'm doing wrong?

James
  • 1,653
  • 2
  • 31
  • 60

1 Answers1

2

The key is encrypted, you need a password (and presumably non-empty so) to decrypt the private key, i.e.

key = paramiko.RSAKey(data=base64.b64decode(b"""AAAAB3Nza..."""), password='my key password')

The server there only allows public key authentication, so giving password to client.connect does not make any sense.