0

I'm working on python automation to check if a specified file available in remote server. Here the module which am using is pysftp. But it is getting failed with below error.

  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'

The implementation:

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

It would be greatly appreciated if someone could identify what is missing in my code snipnet for success connection. Thanks in advance

Ark
  • 67
  • 7

0 Answers0