Python自动打jar包、复制jar包、文件上传到Linux、从Linux下载文件

如需转载分享,请标明出处,且不用于盈利为目的,谢谢合作!

Python自动打jar包、复制jar包、文件上传到Linux、从Linux下载文件

一、自动打jar包

# coding=utf-8
import os,time
time.sleep(1)
os.system("cd D:\IdeaWorkSpaces\\project\\config-server && d: && mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true")
print("成功1!!!")

二、复制jar包

# coding=utf-8
import time,os
t = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
command = "cd D:\最新jar包 && d: && md ygj_java_"+t+"_jar"
os.system(command)
print("创建文件成功!!!")
time.sleep(2)

os.system("copy D:\IdeaWorkSpaces\\project\\config-server\\target\\config-server-2.0.0-SNAPSHOT.jar D:\最新jar包\ygj_java_"+t+"_jar")
print("成功1!!!")
time.sleep(1)

三、文件上传到Linux

import paramiko

import datetime

import os

hostname = 'ip地址'
username = 'root'
password = 'root'
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)
def mkdir(remote_dir):
    try:
        ssh=paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname,username=username,password=password)
        sftp = ssh.open_sftp()
        sftp.mkdir(remote_dir)
        print("Create folder 'remote_check' in remote hosts successfully!\n")
        ssh.close()
    except:
        print("Create folder failure!\n")

if __name__ == '__main__':
    local_dir = r'D:\最新jar包\ygj_java_20181106103315_jar'# 本地路径
    remote_dir = '/root/ygj_java_20181106103315_jar/'#远程路径
    mkdir(remote_dir)
    upload(local_dir, remote_dir)

四、从Linux下载文件

import paramiko


def remote_scp(host_ip, remote_path, local_path, username, password):
    t = paramiko.Transport((host_ip, 22))
    t.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    src = remote_path
    des = local_path
    sftp.get(src, des)
    t.close()


if __name__ == '__main__':
    host_ip = 'ip地址'
    remote_path = '/home/share/a.pdf'
    local_path = r'D:\aaa\\'
    username = 'root'
    password = '******'
    remote_scp(host_ip, remote_path, local_path, username, password)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值