0

I encountered a problem connecting to the server. I'm in this topic for the first time, so I don't understand why I can't connect, it seems that I entered all the data. I Googled and realized that I needed a key, then I got the key in puttygen. But it didn't help.

code:

import pysftp
from base64 import decodebytes
import paramiko

# cnopts = pysftp.CnOpts()

# cnopts.hostkeys = None

cnopts = pysftp.CnOpts(knownhosts='ssh-rsa 2048 SHA256:PEFk7nDBYtqlmNC4VIA8qSx/ii2Gn9T5NuVKHRLM72s')

srv = pysftp.Connection(host="193.34.144.226", username="root", password="**********", cnopts = cnopts)

data = srv.listdir()

srv.close()

for i in data:
    print(i)

traceback:

C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from 193.34.144.226 ssh-rsa 2048 SHA256:PEFk7nDBYtqlmNC4VIA8qSx/ii2Gn9T5NuVKHRLM72s. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg, UserWarning)
Traceback (most recent call last):
File "D:\python\FREELANCE\qa_parser\answers_parser\server_requests.py", line 11, in
srv = pysftp.Connection(host="193.34.144.226", username="root", password="*********", cnopts = cnopts)
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host 193.34.144.226 found.
Exception ignored in:
Traceback (most recent call last):
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 1013, in __del__
self.close()
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
buymyopps
  • 61
  • 1
  • 2
  • @MartinPrikryl, ...the OP here appears to be trying to follow that advice (hence the `knownhosts='...'` value); though whether the value they're passing is _actually correct for the given host_ is a different question, and one I'm not sure we have enough information to answer. – Charles Duffy May 22 '21 at 15:18
  • @buymyopps, can you describe how you got the `ssh-rsa 2048 SHA256:PEFk7nDBYtqlmNC4VIA8qSx/ii2Gn9T5NuVKHRLM72s` value? Usually a known_hosts file is in a different format -- first the hostname, then the type, then the public side of the host key; what you're showing here doesn't have any hostname field at all, which makes it smell likely to be incorrect. – Charles Duffy May 22 '21 at 15:21
  • (one doesn't typically use puttygen to build host keys, since any reasonably-packaged ssh server will create them automatically during startup if they don't already exist; see the `ssh-keyscan` tool to collect public keys used by existing hosts). – Charles Duffy May 22 '21 at 15:22
  • @CharlesDuffy What goes to the `knownhosts` parameter is a file name, not key itself. As my answer to the linked question shows. If OP wants to use a key from a string instead of a file, OP needs to use `cnopts.hostkeys.add`, again, as my answer shows. – Martin Prikryl May 22 '21 at 16:07
  • @MartinPrikryl, thank you -- that makes the applicability of the linked question much more clear. – Charles Duffy May 22 '21 at 20:29

0 Answers0