3

I am trying to connect to an SFTP site using python but the known_hosts file does not exist on my windows 10 machine. I found online that the host key appears when first connecting to the server: https://winscp.net/eng/docs/ssh_verifying_the_host_key

How do I convert this into a known_hosts file that I can use in my python code?

I tried creating a file like: hostname ssh-rsa 2EP...8MZ in the example above but I receive an invalid host key error: Error('Incorrect padding'))

Thank you.

import pysftp

cnopts = pysftp.CnOpts(knownhosts='C:\\Users\\Documents\\known_hosts')

with pysftp.Connection('xxx',username='xxx', password='xxx',cnopts=cnopts) as sftp:
    localpath='TEST.txt'
    remotepath='TEST.txt'
    sftp.put(localpath,remotepath)
    sftp.close()

2 Answers2

0

First you'll want to really know what protocol you are using. In your question you mentionned multiple times FTP, and your code + the concept you're talking about (known_hosts) are SFTP things.

FTP and SFTP are two completely different protocoles, FTP being one for transfering file between a client and a server and SFTP to move files between an SSH client and server.

Kureteiyu
  • 529
  • 4
  • 10
0

SOLVED!!! So I'm on windows and use FileZilla and WinScp for sftp transfers but the C:\Users....ssh file has never existed as far as I'm aware. I downloaded the Windows SSH client: https://www.howtogeek.com/336775/how-to-enable-and-use-windows-10s-built-in-ssh-commands/

And then used PowerShell to connect to the server. When you enter the command: ssh hostname it will state the RSA fingerprint and ask if you want to connect. Enter Yes and it will return "Warning: Permanently added 'host name' to the list of known hosts. The .ssh file will then appear in your C:\Users....ssh directory. This can then be referenced in python.

Thank you.