-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More beginner tips for SFTP server docs #709
Comments
In the Specifying known hosts section you can read that:
Personaly i'd go for using known_hosts = asyncssh.SSHKnownHosts(host_key)
...
connect(
...
known_hosts=known_hosts,
....
) |
Thanks for the response. I assume host_key here is the public key ( |
You need to keep in mind that the known_hosts format is more than just the contents of a public key. It needs to be preceded by a host pattern, and that can optionally be preceded by directive like At a minimum, if you wanted to try a specific public key for all hosts (which could be ok in something like a unit test), you'd need to prefix the public key data with a If you encode the known hosts data in binary mode (as a |
On another note, you don't need to use ssh-keygen to generate keys. If you like, you can use asyncssh.generate_private_key to do that. There are methods on the generated key to write out either the private or public keys, in a variety of formats. |
Adding
This works great! Writing here for future readers import asyncssh
alg = "ssh-rsa"
user_private_key = asyncssh.generate_private_key(alg)
user_public_key = user_private_key.export_public_key().decode()
server_authorized_keys = asyncssh.SSHAuthorizedKeys()
server_authorized_keys.load(user_public_key)
server_private_key = asyncssh.generate_private_key(alg)
server_public_key = server_private_key.export_public_key().decode()
async with asyncssh.listen(
...
public_key_auth=True,
server_host_keys=[server_private_key],
authorized_client_keys=server_authorized_keys,
...
)
private_key: str | None
passphrase: str | None
client_keys = []
if private_key:
client_keys.append(
asyncssh.import_private_key(private_key, passphrase=passphrase)
)
async with asyncssh.connect(
...
client_keys=client_keys,
known_hosts=asyncssh.SSHKnownHosts(f"localhost {server_public_key}"),
...
) Thanks so much for the support; this is such a thorough package 😍 |
Happy to help! You should be able to do the following regarding known_hosts: alg = 'ssh-rsa'
server_private_key = asyncssh.generate_private_key(alg)
server_public_key = server_private_key.export_public_key()
async with asyncssh.connect(
...
known_hosts=b'localhost ' + server_public_key,
...
) Your example here actually had The other option to avoid including internals would be to call |
I was pretty confused by the parameter names of the SSH server, so I thought I'd leave some pointers for anyone else having trouble
To set up an AsyncSSH SFTP server, you can follow these steps:
Generate SSH Keys:
Generate a host key for the server:
Generate a user key:
Write the Server Code:
Use the following example to set up a simple SFTP server:
I needed to run this in pytest so:
you may come across
Host key is not trusted for host localhost
Run the following
ssh-keyscan -p 8022 localhost >> ~/.ssh/known_hosts
.A workaround is setting your SFTP client to include a parameter
known_hosts=None
, but that's a security danger.I'm actually struggling to find a workaround in my unit testing since I don't want to explicitly set
~/.ssh/known_hosts
. I have a client that looks like:I tried adding
parameter to the client, but it failed with
TypeError: 'RSAKey' object is not subscriptable
or when specifyingknown_hosts=[host_key]
, I getTypeError: 'RSAKey' object is not iterable
Any hints as to what I should do?
The text was updated successfully, but these errors were encountered: