首页 > 解决方案 > 使用 pysftp 连接到服务器失败

问题描述

我正在研究 python 自动化以检查远程服务器中是否有指定的文件。这里使用的模块是 pysftp。但它因以下错误而失败。

  File "invader_script_paramiko.py", line 7, in <module>
    with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
  File "/root/python3-virtualenv/lib64/python3.6/site-packages/pysftp/__init__.py", line 132, in __init__
    self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
  File "/root/python3-virtualenv/lib64/python3.6/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 hvipk0000015pr.xxxdc.local found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x7f111b6a8e48>>
Traceback (most recent call last):
  File "/root/python3-virtualenv/lib64/python3.6/site-packages/pysftp/__init__.py", line 1013, in __del__
    self.close()
  File "/root/python3-virtualenv/lib64/python3.6/site-packages/pysftp/__init__.py", line 784, in close
    if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'

实施:

import pysftp

myHostname = "hvipk0000015pr.xxxdc.local"
myUsername = "xxxxx\\username_sa"
myPassword = "password"

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
    print("Connection succesfully stablished ... ")

    # Switch to a remote directory
    sftp.cwd('/srv/data/xxx_bperf_knnfs_pr/Check_transit/Dummy')

    # Obtain structure of the remote directory '/var/www/vhosts'
    directory_structure = sftp.listdir_attr()

    # Print data
    for attr in directory_structure:
        print(attr.filename, attr)
    
# connection closed automatically at the end of the with-block

如果有人能够识别我的代码片段中缺少的内容以进行成功连接,将不胜感激。提前致谢

标签: python-3.xsshparamikopysftpremote-login

解决方案


推荐阅读