Skip to content

No way to add a host key from a string #1134

Open

Description

@alexreg

It seems there is currently no way to add a host key from a string, but rather only a file. See http://docs.paramiko.org/en/2.4/api/hostkeys.html.

I would suggest either exposing the HostKeys.entries attribute so that HostKeyEntrys can be added directly, or adding some proxy methods on the HostKeys class for adding/removing/updating HostKeyEntrys.

Furthermore, it would be nice if the HostKey class could load from a file-like object.

Activity

  1. alexreg commented on Dec 17, 2017

    @alexreg
    Author

    One more thing: it would be nice if the HostKeyEntry constructor could take a key_type parameter... or is this unnecessary?

  2. ploxiln commented on Dec 17, 2017

    @ploxiln
    Contributor
  3. alexreg commented on Dec 17, 2017

    @alexreg
    Author

    Ah right. Slightly non-intuitive, but fair enough. I had just figured out the paramiko.RSAKey(data=b"key byte string") bit myself, but that's not so obvious, so perhaps could go in the docs. I used a base64 decode too, to convert from the standard OpenSSH string format.

  4. benjimin commented on Jul 23, 2026

    @benjimin

    I think it makes sense to allow a key as a string, without requiring the caller to know which supported crypto algorithm it uses.

    For example, the application code may be loading the key (directly into memory) from some secure secret store and not wish to persist it onto a filesystem (as even a temp file increases exposure to credential leak), and if the key is updated in future to use a different supported crypto algorithm (say if the credential migrates from RSA to Ed25519 during a routine key rotation) the same application code should still work without modification.

    Ideally we should be able to write:

    paramiko.SSHClient().connect(hostname="example.com", username="jane", key=my_private_key_string)

    Instead of needing to write:

    for alg in [paramiko.RSAKey, paramiko.ECDSAKey]:
      try:
        key = alg.from_private_key(io.StringIO(my_private_key_string))
      except:
        continue
      else:
        break
    paramiko.SSHClient().connect(hostname="example.com", username="jane", pkey=key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions