0

I am using Python 2.7 with library paramiko. I want to find the latest modified file in the remote machine. Here is my code:

First, I import the required libraries,

import paramiko

Second, I set up the ssh client and sftp,

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("XXX.XXX.XXX.XX",22,username="NAME",password='PW',timeout=4)

sftp = s.open_sftp()

Afterwards, I have no idea how I can traverse the directory /home/image/ to find out the latest modified file in the remote machine.

I only know how to find it in local, like:

file= max(glob.iglob(os.path.join('/home/image/','*.png')), key=os.path.getmtime).replace("//","/")

I am asking for help how can I find out the latest modified file in the remote machine. Thank you.

VICTOR
  • 1,894
  • 5
  • 25
  • 54

1 Answers1

0

Finally I got the solution. Thanks for the hints from @Burhan Khalid.

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("XXX.XXX.XX",22,username="NAME",password='PW',timeout=4)
sftp = s.open_sftp()

stdin, stdout, stderr = s.exec_command('cd /home/image; ls -1t | head -1') 
for line in stdout.read().splitlines():
    print line
VICTOR
  • 1,894
  • 5
  • 25
  • 54
  • 2
    Well, this is not SFTP solution. You are using remote shell and rely on a specific operating system, or even shell. Note that you never use the `sftp` variable. – Martin Prikryl Jun 17 '16 at 05:28