Search code examples
python-3.xsshsftpfile-copying

Read file from remote server completely to local machine in python SSHCLient?


My problem is that file differs in size anywhere from 1000 lines to 10000 lines and the below method doesn't work as it copies only a few lines and closes

client = SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(drasclientip, username=def_users[i], key_filename=pem_file)
        sftp = client.open_sftp()
        time.sleep(10)
        sftp.get(remote_file, file_dst,None)
        time.sleep(20)
        sftp.close()
        client.close()

I am only interested in the remote serves file which has some text at the end of the file.

I am thinking 2 ways - either make the file transfer faster and efficient or read the remote file in reverse - that way I can get the end results at least

above code I put delays and it works something.


Solution

  • So here is what worked for me.

    1. since the file was too big to read via ssh client from remote server and I was only looking for file completion indication, which i can see only at EOF.
    2. my startegy was to connect to remote server and run 'tac' linux command- which reverse the file and then i accessed the reversed file and found the EOF results in beginning of file and i was able to confirm the Validation of file

    code:

    *SSH client connect to remote server

    ssh.exec("tac" + filename.txt + " >> filereversed.txt" )
    

    *read the filereversed.txt from remote server and validate