脚本如下:

# cat myscp.py 
#!/usr/bin/env python

import paramiko
import os

hostname  = '192.168.56.101'
port      = 22
username  = 'root'
password  = '111111'
dir_path  = '/root/perl'

if __name__ == "__main__":
    t = paramiko.Transport((hostname, port))
    t.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    files = sftp.listdir(dir_path)
    for f  in files:
        print 'Retrieving', f
        sftp.get(os.path.join(dir_path, f), f)

    t.close()


执行结果如下:

# python myscp.py 
Retrieving 10_29.pl
Retrieving 10_30.pl
Retrieving 10_28.pl
Retrieving 10_27.pl

# ls *.pl
10_27.pl  10_28.pl  10_29.pl  10_30.pl


使用paramiko模块复制远程文件到本地_scp