python sftp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sftp
 
s_file  =   path.join(path_name,name).replace( '\\',' / ')
def  process_sftp_dir(path_name):
                 """
                 此函数递归处理sftp server端的目录和文件,并在client端创建所有不存在的目录,然后针对每个文件在两端的全路径执行get操作.
                 path_name第一次的引用值应该是source_path的值
                 """
                 d_path  =  path_name.replace(source_path,destination_path, 1 )
                 if  not   path.exists(d_path):     # 若目标目录不存在则创建
                     print ( '%s----Create Local Dir: %s'  %  ( ' ' * 8 ,d_path))
                     try :
                          makedirs(d_path)     # 递归创建不存在的目录
                     except  Exception as err:
                         print ( '%s----Create %s Failed'  %  ( ' ' * 8 ,d_path))
                         print ( '{}----{}' . format ( ' ' * 8 ,err))
                         exit( 10 )
                 for  name  in  (i  for  in  sftp.listdir(path = path_name)  if  not  i.startswith( '.' )):
                     """去掉以.开头的文件或目录"""
                     s_file  =   path.join(path_name,name).replace( '\\',' / ')    # 在win环境下组合路径所用的' \\ '换成' / '
                     d_file  =  s_file.replace(source_path,destination_path, 1 )     # 目标端全路径
                     chk_r_path_result  =  check_remote_path(s_file)
                     if  chk_r_path_result  = =  'file' :     # 文件
                         sftp_get(s_file,d_file, 12 )
                     elif  chk_r_path_result  = =  'directory' :     # 目录
                         process_sftp_dir(s_file)     # 递归调用本身
             process_sftp_dir(source_path)

参考

http://kaifly.blog.51cto.com/3209616/1832200

http://wangwei007.blog.51cto.com/68019/1285412


sftp:

sftp.listdir

s_file =  path.join(path_name,name).replace('\\','/') 

指定源全路径下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
第一个例子
 
from  paramiko  import  SSHClient, AutoAddPolicy
from  os  import  path, walk, makedirs
from  re  import  split, match, search
from  sys  import  exit
import  datetime
 
server_ip = '192.168.1.100'
port = 22
user = 'root'
password = '123456'
client  =  SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
try :
     client.connect(server_ip, port = port, username = user, password = password)
except  Exception as err:
     print ( '{}----{} error: {}' . format ( ' ' * 4 ,server_ip,err))
sftp  =  client.open_sftp()
 
def  sftp_transfer_rcmd(cmd = None , space = None ):
     stdin, stdout, stderr  =  client.exec_command(cmd)
     copy_out, copy_err  =  stdout.readlines(), stderr.readlines()
     if  len (copy_err) ! =  0 :
         for  in  copy_err:
             # print('%s----%s' % (' ' * space, i), end='')
             print  i
             exit( 10 )
     else :
         return  copy_out
 
def  check_remote_path(r_path):
     check_cmd  =  'if [ -e {0} ];then if [ -d {0} ];then echo directory;elif [ -f {0} ];then echo file;fi;else echo no_exist;fi' . format (
         r_path)
     check_result  =  sftp_transfer_rcmd(cmd = check_cmd)[ 0 ].strip( '\n' )
     if  check_result  = =  'directory' :
         return  'directory'
     elif  check_result  = =  'file' :
         return  'file'
     else :
         return  'no_exist'
 
'''
#d:/ftp/opt/...
def process_sftp_dir(src_dir,dest_dir):
     for name in (i for i in sftp.listdir(path=dest_dir) if not i.startswith('.')):
         dest_dir_file=path.join(dest_dir,name).replace('\\','/')
         result=check_remote_path(dest_dir_file)
         src_dir_files = src_dir.replace('\\', '/')
         src_dir_file = path.join(src_dir_files, dest_dir_file[1:]).replace('\\', '/')
         if result == 'directory':
             dest_dir_dirs = path.join(src_dir_files,dest_dir_file[1:]).replace('\\', '/')
             if not path.exists(dest_dir_dirs):
                 makedirs(dest_dir_dirs)
             process_sftp_dir(src_dir, dest_dir_file)
         elif result == 'file':
             print src_dir_file
             sftp.get(dest_dir_file,src_dir_file)
 
'''
 
 
'''
d:/ftp/test/...
'''
def  down_sftp_dir(source_path,destination_path):
     for  name  in  (i  for  in  sftp.listdir(path = source_path)  if  not  i.startswith( '.' )):
         s_file  =  path.join(source_path, name).replace( '\\', ' / ')
         result  =  check_remote_path(s_file)
         d_file  =  s_file.replace(source_path, destination_path,  1 )
         if  result  = =  'directory' :
             if  not  path.exists(d_file):
                 makedirs(d_file)
             down_sftp_dir(s_file, d_file)
         elif  result  = =  'file' :
             sftp.get(s_file, d_file)
 
def  upload_sftp_dir(source_path,destination_path):
     for  root, dirs, files  in  walk(source_path):
         for  filespath  in  files:
             local_file  =  path.join(root, filespath)
             =  local_file.replace(source_path, '')
             remote_file  =  path.join(destination_path, a).replace( '\\', ' / ')
             try :
                 sftp.put(local_file, remote_file)
             except  Exception, e:
                 sftp.mkdir(path.split(remote_file)[ 0 ])
                 sftp.put(local_file, remote_file)
             print  "upload %s to remote %s"  %  (local_file, remote_file)
         for  name  in  dirs:
             local_path  =  path.join(root, name)
             =  local_path.replace(source_path, '')
             remote_path  =  path.join(destination_path, a).replace( '\\', ' / ')
             try :
                 sftp.mkdir(remote_path)
                 print  "mkdir path %s"  %  remote_path
             except  Exception, e:
                 print  e
     print  'upload file success %s '  %  datetime.datetime.now()
 
 
 
 
if  __name__ = = '__main__' :
     destination_path = 'd:\\ftp\\'
     source_path = '/opt/ftp/'
     upload_sftp_dir(destination_path, source_path)
     client.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
第二个例子
parent, child  =  os.path.split(s_file)
print  parent,child
/ tmp / test / testno testno
 
#!/usr/bin/python   
import  pexpect
import  wparamiko
import  os
import  sys
import  time
import  multiprocessing
import  datetime
import  crash_on_ipy
from  stat  import  S_ISDIR
   
ip_list  =  []
#room_id = sys.argv[1]
 
 
class  run_cmd():
       def  __init__( self ,hostname = None ,password = None ,username = None ,port = None ,echo_cmd = None ):
           #threading.Thread.__init__(self)
           self .hostname = hostname
           self .password = password
           self .username = username
           self .port = port
           self .echo_cmd = echo_cmd
           #self.thread_stop=False
       def  run( self ):
           wparamiko.util.log_to_file( 'paramiko.log' )
           s = wparamiko.SSHClient()
           s.set_missing_host_key_policy(wparamiko.AutoAddPolicy())
           s.connect(hostname  =  self .hostname,username = self .username, password = self .password)
           stdin,stdout,stderr = s.exec_command( self .echo_cmd)
           return  stdout.readlines()
           s.close()
       def  stop( self ):
            self .thread_stop = True
 
 
class  get_thread():
     def  __init__( self ,hostname,password,username,port = None ):
    #def __init__(self,hostname,username='root',key_file=None,password=None): 
    #def __init__(self,hostname=None,password=None,username=None,port=None,local_dir=None,remote_dir=None):
         self .hostname  =  hostname
         self .username  =  username
         self .password  =  password
         self .scp  =  wparamiko.SSHClient()
         self .scp.set_missing_host_key_policy(wparamiko.AutoAddPolicy())
         self .scp  =  wparamiko.Transport((hostname,  22 ))
         self .scp.connect(username = username, password = password)
         #self.scp.connect(username='tomcat', password='faJxjj/scadmin^o2o&f8com1')
         self .sftp  =  wparamiko.SFTPClient.from_transport( self .scp)
     def  _walk_remote( self , dirpath):
         dirnames  =  []
         filenames  =  []
 
         for  fd  in  self .sftp.listdir_attr(dirpath):
             if  S_ISDIR(fd.st_mode):
                 dirnames.append(fd.filename)
             else :
                 filenames.append(fd.filename)
         yield  dirpath, dirnames, filenames
 
         for  dirname  in  dirnames:
             new_dirpath  =  os.path.join(dirpath, dirname)
             # yield from self._walk_remote(new_dirpath)
             for  walk  in  self ._walk_remote(new_dirpath):
                 yield  walk
 
     def  getall( self ,local,remote):
         
         st_mode  =  self .sftp.stat(remote).st_mode
         if  not  S_ISDIR(st_mode):
             filename  =  os.path.basename(remote)
             self .sftp.get(remote, os.path.join(local, filename))
         else :
             parent, child  =  os.path.split(remote)
 
             for  dirpath, dirnames, filenames  in  self ._walk_remote(remote):
                 dirpath  =  dirpath.replace(parent,  '.' )
                 parentc  =  os.path.join(local,dirpath)
                 if  not  os.path.exists(parentc):
                   os.makedirs(parentc)
                 for  dirname  in  dirnames:
                     try :
                         os.makedirs(os.path.join(local, dirpath, dirname))
                     except :
                         pass
 
                 for  filename  in  filenames:
                     localpath  =  os.path.join(local, dirpath, filename)
                     remotepath  =  os.path.join(parent, dirpath, filename)
                     self .sftp.get(remotepath, localpath)
         self .scp.close()
if  __name__ = = '__main__' :
     port  =  22
     now  =  datetime.datetime.now()
     strdatetime  =  now.strftime( "%Y-%m-%d" )
     year = strdatetime.split( '-' )[ 0 ]
     mon = strdatetime.split( '-' )[ 1 ]
     day = strdatetime.split( '-' )[ 2 ]
     Datenow1 =  year  +  "/"  +  mon  +  "/"  +  day  +  "/"
     Datenow =  year  +  "/"  +  mon
     print  "-" * 50
     #getthread=get_thread()
     #room_pathd = '/opt/src/logs/crm/'
     #room_paths = '/home/python/'
     =  file ( '/home/python/filelist' , 'r' )
     =  f.readlines()
     for  in  c:
         hostname  =  x.split( '::' )[ 0 ]
         password  =  x.split( '::' )[ 1 ]
         username  =  x.split( '::' )[ 2 ]
         local =  x.split( '::' )[ 3 ].strip( '\n' )
         remotes  =  x.split( '::' )[ 4 ].strip( '\n' )
         localz = local  +  "/"  +  mon  +  "/"  +  day
         if  remotes.endswith( '/' ):
             remote1  =  remotes  +  Datenow
             remote2  =  remotes  +  Datenow1
         else :
             remote3  =  remotes
         if  not  os.path.exists(localz):
             remote  =  remote1
             getthread = get_thread(hostname,password,username)
             getthread.getall(local,remote)
         else :
             remote  =  remote2
             echo_cmd = '/bin/find %s -maxdepth 1 -type d -mmin -50'  %  (remote)
             cmd_thread = run_cmd(hostname,password,username,port,echo_cmd)
             result = cmd_thread.run()
             del  result[ 0 ]
             for  item  in  result:
                 print  str (item)
                 items  =  item.strip( '\n' )
                 getthread = get_thread(hostname,password,username)
                 getthread.getall(localz,items)
                 #getthread.getall(localz,'/opt/src/logs/service/o2o-admin/2016/10/28/test')
 
         
     f.close()
    #getthread.getall(room_paths,room_pathd)
 
主要os.path.join搞得头大
1
2
3
4
5
6
7
8
9
10
11
12
13
# !/usr/bin/env python
# -*-coding:utf-8-*-
import  os,sys
local = '/home/logs/a/'
remote = '/opt/src/logs/a/test-dmin/'
#remote='/opt/src/logs/a/test-dmin' 这两者结果是不一样的
parent, child  =  os.path.split(remote)
print  parent
dirpath = remote
dirpath  =  dirpath.replace(parent,  '.' )
dirname = 'test/test2'
print  local,dirpath,dirname
print  os.path.join(local, dirpath, dirname)



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1908148,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值