python sftp

2023-01-31 02:01:14 python sftp
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 i 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('\\','/') 

指定源全路径下载

第一个例子

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 i 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 i 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)
            a = 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)
            a = 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()
第二个例子
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/'
    f = file('/home/python/filelist','r')
    c = f.readlines()
    for x 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搞得头大
# !/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)


相关文章