Search code examples
pythonpython-3.xfabricprivate-key

Question about connection by private key with python fabric


How I can specify pass phrase for my Connection with private key? I didn't find anything about this in fabric documentation.

Here is my connection:

c = Connection(host="host",user="usr",connect_kwargs={"key_filename": "/home/user/.ssh/pkey",},)

And error:

paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted


Solution

  • As per documentation says:

    The connect_kwargs.passphrase config option is the most direct way to supply a passphrase to be used automatically.

    Quick fix:

    c = Connection(host="host",user="usr",connect_kwargs={"key_filename": "/home/user/.ssh/pkey", "passphrase": "demo"},)
    

    Here you can find more details. Also in here.

    The recommended approach is to export an environment variable in your shell session. e.g. export SSH_PASSPHRASE="gongo-aso!"

    This can then be read from and used in the connect_kwargs options passed to the constructor for the Connection object.

    connect_kwargs = {
        'passphrase': getenv('SSH_PASSPHRASE')
    }
    
    c = Connection('[email protected]', connect_kwargs=connect_kwargs)