昊天SEO

python中paramiko模块 远程主机批量下载文件到本机

代码如下 
#!/usr/bin/env python 
import paramiko 
import os 
import datetime 
from ConfigParser import ConfigParser 
ConfigFile='config.ini' 
config=ConfigParser() 
config.read(ConfigFile) 
hostname1=''.join(config.get('IP','ipaddress')) 
address=hostname1.split(';') 
print address 
username='root' 
password='itpschina123' 
port=22 
local_dir='/tmp/' 
remote_dir='/tmp/test/' 
if __name__=="__main__": 
 # try: 
 for ip in address: 
 t=paramiko.Transport((ip,port)) 
 t.connect(username=username,password=password) 
 sftp=paramiko.SFTPClient.from_transport(t) 
# files=sftp.listdir(dir_path) 
 files=os.listdir(local_dir) 
 print files 
 for f in files: 
 print '####################################################' 
 print 'Begin to upload file to %s ' % ip 
 print 'Uploading ',os.path.join(local_dir,f) 
 
 print datetime.datetime.now() 
 sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f)) 
 print datetime.datetime.now() 
 print '####################################################' 
 t.close()

下面是config.ini的配置文件内容:

  1. [IP]
  2. #ipaddress = 192.168.0.102;192.168.0.103
  3. ipaddress = 74.63.229.*;69.50.220.*

本文地址官网    新乡网站建设,seo

分享到:更多 ()
a