Python 操作Windows文件上传Linux,Linux文件下载到Windows本地

关注点


使用python操作从linux服务器下载文件到Windows指定路径

#!/usr/local/bin/python
# encoding:utf-8

import paramiko
import os

HOST_IP='10.0.55.125'
REMOTE_PATH='/proc'
REMOTE_FILENAME='meminfo'
LOCAL_PATH='G:\06-company_program\file_model'
USERNAME='root'
PASSWORD='Qwe123!!'

def remote_scp(host_ip,remote_path,local_path,file_name,username,password):
    t = paramiko.Transport((host_ip,22))
    t.connect(username=username, password=password) # 登录远程服务器
    sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议
    src = remote_path+'/'+file_name
    des = local_path+'/'+file_name
    sftp.get(src,des)
    t.close()

if not os.path.isdir(LOCAL_PATH):
    os.makedirs(LOCAL_PATH)

if not os.path.isfile(LOCAL_PATH+'/'+REMOTE_FILENAME):
    fp=open(LOCAL_PATH+'/'+REMOTE_FILENAME,'w')
    fp.close()
    remote_scp(HOST_IP,REMOTE_PATH,LOCAL_PATH,REMOTE_FILENAME,USERNAME,PASSWORD)
import paramiko
import NoValidConnectionsError, AuthenticationException, SSHException

class SshRemoteConnection:
    def __init__(self, hostname, port, user, password):
        self.hostname = hostname
        self.port = port
        self.user = user
        self.password = password
    def do_connect(self, conn):    
        """创建连接"""    
        if conn == "SSHClient":        
            client = paramiko.SSHClient()        
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())        
            try:            
                client.connect(hostname=self.hostname,port=self.port,username=self.user,password=self.password)
                print("正在连接 {}".format(self.hostname).center(100, '*'))       
            except NoValidConnectionsError as e:            
                print(f"连接失败:{e}".center(100, '*'))        
            except AuthenticationException as e:            
                print(f"密码错误:{e}".center(100, '*'))        
            return client    
        elif conn == "Transport":        
            # 获取Transport实例        
            tran = paramiko.Transport(self.hostname, int(self.port))        
            try:            
            # 连接SSH服务端            
                tran.connect(username=self.user, password=self.password)        
            except SSHException as e:            
                print(f'连接失败:{e}'.center(100, '*'))        
            return tran

    def do_get_all_files(self, remote_path, local_path):   
        """批量下载目录下所有文件"""    
        print('正在批量下载文件...'.center(100, '*'))    
        conn = self.do_connect('Transport')    
        sftp = paramiko.SFTPClient.from_transport(conn)    
        all_files = self.__get_all_remote_files(sftp, remote_path)    
        # 依次下载每一个文件    
        for file in all_files:        
            filename = file.split('/')[-1]        
            local_filename = os.path.join(local_path, filename)        
            print(f'{filename} 文件正在传输中...'.center(100, '*'))        
            sftp.get(file, local_filename)    
        print('传输完成...'.center(100, '*'))    
        conn.close()

if __name__ == "__main__":    
    s = SshRemoteConnection('10.0.55.125', 22, 'root', "Qwe123!!")      # hostname, port, user, password   
    localpath = 'G:\06-company_program\file_model'  
    remotepath = '/data/20210719'     
    s.do_get_all_files(remotepath, localpath)    


从Windows上传文件到linux目录下,文件夹自动创建

import paramiko  
import datetime 
import os  
 
hostname='10.xxx.xxx.xx'  
username='username'  
password='***'  
port=22  
def upload(local_dir,remote_dir):  
    try:  
        t=paramiko.Transport((hostname,port))  
        t.connect(username=username,password=password)  
        sftp=paramiko.SFTPClient.from_transport(t)  
        print('upload file start %s ' % datetime.datetime.now())  
        for root,dirs,files in os.walk(local_dir):  
            print('[%s][%s][%s]' % (root,dirs,files))  
            for filespath in files:  
                local_file = os.path.join(root,filespath)  
                print(11,'[%s][%s][%s][%s]' % (root,filespath,local_file,local_dir))  
                a = local_file.replace(local_dir,'').replace('\\','/').lstrip('/')  
                print('01',a,'[%s]' % remote_dir)  
                remote_file = os.path.join(remote_dir,a)  
                print(22,remote_file)  
                try:  
                    sftp.put(local_file,remote_file)  
                except Exception as e:  
                    sftp.mkdir(os.path.split(remote_file)[0])  
                    sftp.put(local_file,remote_file)  
                    print("66 upload %s to remote %s" % (local_file,remote_file))  
            for name in dirs:  
                local_path = os.path.join(root,name)  
                print(0,local_path,local_dir)  
                a = local_path.replace(local_dir,'').replace('\\','')  
                print(1,a)  
                print(1,remote_dir)  
                remote_path = os.path.join(remote_dir,a)  
                print(33,remote_path)  
                try:  
                    sftp.mkdir(remote_path)  
                    print(44,"mkdir path %s" % remote_path)  
                except Exception as e:  
                    print(55,e)  
        print('77,upload file success %s ' % datetime.datetime.now())  
        t.close()  
    except Exception as e:  
        print(88,e)  
          
if __name__=='__main__':  
     local_dir=r'D:\test'  
     remote_dir='/home/share/test/'  
     upload(local_dir,remote_dir)  

一个操作Linux的代码:

# -*- coding: utf-8 -*-
"""
@File    : sshclient.py
@Software: PyCharm 
@Comment : 
"""
import osfrom stat 
import S_ISDIR
import re
import paramikofrom paramiko.ssh_exception 
import NoValidConnectionsError, AuthenticationException, SSHException

class SshRemoteConnection:
    def __init__(self, hostname, port, user, password):
        self.hostname = hostname
        self.port = port
        self.user = user
        self.password = password
    def do_connect(self, conn):    
        """创建连接"""    
        if conn == "SSHClient":        
            client = paramiko.SSHClient()        
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())        
            try:            
                client.connect(hostname=self.hostname,port=self.port,username=self.user,password=self.password)
                print("正在连接 {}".format(self.hostname).center(100, '*'))       
            except NoValidConnectionsError as e:            
                print(f"连接失败:{e}".center(100, '*'))        
            except AuthenticationException as e:            
                print(f"密码错误:{e}".center(100, '*'))        
            return client    
        elif conn == "Transport":        
            # 获取Transport实例        
            tran = paramiko.Transport(self.hostname, int(self.port))        
            try:            
            # 连接SSH服务端            
                tran.connect(username=self.user, password=self.password)        
            except SSHException as e:            
                print(f'连接失败:{e}'.center(100, '*'))        
            return tran
    def do_cmd(self, cmd):    
        """执行shell命令,多条命令以分号‘;’隔开"""    
        # 连接    
        conn = self.do_connect('SSHClient')    
        # 执行操作    
        stdin, stdout, stderr = conn.exec_command(cmd, get_pty=True)    
        # 获取命令执行的结果    
        res, err = stdout.read().decode('utf-8'), stderr.read().decode('utf-8')    
        result = res if res else err    
        print('执行成功'.center(100, '*'))    
        print(result)    
        return result
    def do_put_file(self, local_path, remote_path):    
        """上传单个文件"""
        print('正在上传文件...'.center(100, '*'))
        conn = self.do_connect('Transport')
        sftp = paramiko.SFTPClient.from_transport(conn)
        try:
            sftp.put(local_path, remotepath=remote_path) 
            print(f'{local_path}文件上传到{self.hostname}主机的{remote_path}')
        except SSHException as e:        
            print(f'上传失败:{e}'.center(100, '*'))    
            conn.close()
    def __get_all_local_files(self, local_dir):    
        """获取需要批量上传目录下的所有文件"""    
        all_files = list()    
        files = os.listdir(local_dir)    
        for i in files:       
            local_filename = os.path.join(local_dir, i)        
            if os.path.isdir(i):            
                all_files.extend(self.__get_all_local_files(local_filename))        
            else:            
                all_files.append(local_filename)    
            return all_files
    def do_put_all_files(self, local_path, remote_path):    
        """批量上传所有文件"""    
        print('正在上传文件...'.center(100, '*'))    
        conn = self.do_connect('Transport')    
        sftp = paramiko.SFTPClient.from_transport(conn)    
        # 去掉路径字符串最后的字符'/',如果有的话    
        if remote_path[-1] == '/':        
        remote_path = remote_path[0:-1]    
        # 获取本地目录下的所有文件    
        all_files = self.__get_all_local_files(local_path)    
        try:        
            for file in all_files:            
                local_filename = os.path.split(file)[-1]            
                remote_filename = remote_path + '/' + local_filename            
                print('{} 文件正在传输中...'.center(100, '*').format(remote_filename))            
                sftp.put(file, remote_filename)            
                print('文件传输成功...'.center(100, '*').format(remote_filename))    
        except SSHException as e:        
            print(f'上传失败:{e}'.center(100, '*'))    
        conn.close()
    def do_get_file(self, remote_path, local_path):    
        """下载文件"""    
        print('正在下载文件...'.center(100, '*'))    
        conn = self.do_connect('Transport')    
        sftp = paramiko.SFTPClient.from_transport(conn)    
        try:        
            sftp.get(remotepath=remote_path, localpath=local_path)        
            print(f'{remote_path}文件从{self.hostname}主机下载到{local_path}成功')    
        except SSHException as e:        
            print(f'下载失败:{e}'.center(100, '*'))    
        conn.close()
    def __get_all_remote_files(self, sftp, remote_dir):    
        """获取需要批量下载目录下的所有文件"""    
        all_files = list()    
        # 去掉路径字符串最后的字符'/',如果有的话    
        if remote_dir[-1] == '/':        
        remote_path = remote_dir[0:-1]    
        files = sftp.listdir_attr(remote_dir)    
        for i in files:
            remote_filename = remote_dir + '/' + i.filename        
            # 判断是不是路径,是路径的话,遍历路径下的文件        
            if S_ISDIR(i.st_mode): 
            all_files.extend(self.__get_all_remote_files(sftp, remote_filename))        
            else:            
                all_files.append(remote_filename)    
                return all_files
    def do_get_all_files(self, remote_path, local_path):   
        """批量下载目录下所有文件"""    
        print('正在批量下载文件...'.center(100, '*'))    
        conn = self.do_connect('Transport')    
        sftp = paramiko.SFTPClient.from_transport(conn)    
        all_files = self.__get_all_remote_files(sftp, remote_path)    
        # 依次下载每一个文件    
        for file in all_files:        
            filename = file.split('/')[-1]        
            local_filename = os.path.join(local_path, filename)        
            print(f'{filename} 文件正在传输中...'.center(100, '*'))        
            sftp.get(file, local_filename)    
        print('传输完成...'.center(100, '*'))    
        conn.close()
    def do_search_file(self, check_dir, checkup=None, fuzzy=True):    
        """校验目录下的文件,默认模糊查询fuzzy=True,匹配忽略大小写"""    
        res = self.do_cmd(f'cd {check_dir};ls')    
        dir_list = [i for i in res.split('\r\n') if i != '']    
        num = 0    
        all_file = []    
        print('正在获取检索结果...'.center(100, '*'))    
        for file in dir_list:        
            if fuzzy:            
                ma = re.compile(f'{checkup}', flags=re.IGNORECASE).findall(f'{file}')        
            else:            
                ma = re.compile(f'{checkup}').findall(f'{file}')        
            if ma:            
                print(file)            
                all_file.append(file)            
                num += 1       
            else:            
                print('...')    
        print(f'检索完毕...找到{num}个结果'.center(100, '*'))    
        return all_file
    def do_review_contents(self):    
        pass


if __name__ == "__main__":    
    s = SshRemoteConnection('99.99.45.123', 22, 'user', "123!@#")    
    s.do_cmd('pwd;ls -l')    
    localpath = 'D:\\00_data\\test'    
    filename = '111.DAT'    
    remotepath = '/data/20210719'    
    remote_filename = filename    
    # s.do_put_all_files(f'{localpath}', f'{remotepath}')    
    # s.do_put_file(f'{localpath}\\{filename}', f'{remotepath}\\{remote_filename}')    
    s.do_get_all_files(remotepath, localpath)    
    s.do_search_file('/data/20210719', 'txt')

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值