How to get latest three files from a remote server via python?
<p>I created ten files with the shell script <code>touch.sh</code> on the server <code>test2</code>:  </p>

<pre><code>root@test2:/sites/buildglasses-qa.walmart.com/ftp/walmart# cat touch.sh 
for i in {1..10}
do 
    touch WMI_Order_Req_409313_$i.xml
    sleep 1
done

root@test2:/sites/buildglasses-qa.walmart.com/ftp/walmart# ls 
touch.sh                     WMI_Order_Req_409313_2.xml  WMI_Order_Req_409313_5.xml  WMI_Order_Req_409313_8.xml
WMI_Order_Req_409313_10.xml  WMI_Order_Req_409313_3.xml  WMI_Order_Req_409313_6.xml  WMI_Order_Req_409313_9.xml
WMI_Order_Req_409313_1.xml   WMI_Order_Req_409313_4.xml  WMI_Order_Req_409313_7.xml
</code></pre>

<p>Server <code>test</code> can connect to remote server <code>test2</code> via ssh without passphrase prompt.<br>
<strong>I want to get latest three files from remote server <code>test2</code> on test.</strong></p>

<p><strong>Use Farbic:</strong><br>
The following is my doing with farbic.I encountered a problem.<br>
Who can give me an solve with <code>farbic</code> Or <code>subprocess</code>,or a better ways? Thanks.</p>

<pre><code>root@test:~# cat fabfile.py 
#!/usr/bin/env python
#-*- coding:utf8 -*-

from fabric.api import *
from fabric.contrib.project import rsync_project

env.user = 'root'
env.host_string =  "test2"


def getremote():
    code_dir = '/sites/buildglasses-qa.walmart.com/ftp/walmart'
    with cd(code_dir):
        cmd = '''ls -lt WMI_Order_Req* |sed -n '1,3p' |awk '{print $9}' |tr "\n" " ";echo'''
        output = run(cmd)
        print output
        get(output,'/tmp')

getremote()
</code></pre>

<p><a href="https://i.stack.imgur.com/btHn2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/btHn2.png" alt="enter image description here"></a></p>

<p><strong>Use Python:</strong></p>

<pre><code>root@test:~# cat get.py
#!/usr/bin/env python
#-*- coding:utf8 -*-
# Power by will yu2016-09-06 10:49:06

import subprocess
import os

cmd="ls -t /sites/buildglasses-qa.walmart.com/ftp/walmart/WMI_Order_Req_*|head -2"
ssh = subprocess.Popen(["ssh", "test2", cmd], shell=False,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print result
</code></pre>

<p><a href="https://i.stack.imgur.com/j7Oks.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j7Oks.png" alt="enter image description here"></a>
I am sad. I donnot know what is next to do.</p>