2

I am trying to connect to an sftp server using python on Windows. I cannot connect to the server without a hostkey. A lot of options, especially with pysftp ask for hostkey files which apparently aren't generated on Windows.

From what I can see is that I have 3 options:

1) [Most preferred] use a string to generate the required key through python code. But that's always producing errors - some of the code using StringIO doesn't work on Python 3 and the possible workarounds don't seem to work for me. Apparently I am not able to decode the key properly and I have no idea how to check for that. I have tried to generate PKey but I am not sure what I am doing wrong.

2) get the right format of known_hosts file (I have no idea what it should look like and couldn't find any samples online) - and put or add the hostkey there. That way I'd (hopefully) be able to use cnopts to connect to the server.

3) This is a fallback option, if the above two don't work: use python to call powershell script that does the work - I have been successful at this but would like to pass arguments from python script to powershell and then get results/status from powershell back to python.

The most relevant options that I have tried out:

Python - pysftp / paramiko - Verify host key using its fingerprint

Embedding public key as string in Paramiko Application

Using python pysftp package, getting a "SSHException: Bad host key from server" error

What I'd like is a sample string (that would look like the actual thing) and the steps around creating a key from it. Or a sample file that I can use to just plugin the details and run the code.

mkhd
  • 21
  • 3
  • My answer to [Python - pysftp / paramiko - Verify host key using its fingerprint](https://stackoverflow.com/q/46814823/850848) (which you link yourself) shows both the format of `known_hosts` file and of the string (they are actually the same). So unless you show us the exact code that you have tried and what exact problem do you have with the code, we can hardly help you. – Martin Prikryl Aug 28 '19 at 18:58
  • Also note that the [Embedding public key as string in Paramiko Application](https://stackoverflow.com/q/27669813/850848) is about a completely different topic. It's about authentication with a key pair, not about verifying the host key. Make sure you understand the difference. See my article on [Understanding SSH key pairs](https://winscp.net/eng/docs/ssh_keys), in case you mistake the *host* and *user* key pairs and use them for opposite purpose. – Martin Prikryl Aug 28 '19 at 19:01

0 Answers0