github:easy-copy

import os
import sys
import time
import paramiko as pm '''
host格式:
{
"ip":"127.0.0.1",
"port":22,
"username":"root",
"password":"123456",
"file":"filename"
}
dst为host列表 要求:
源主机需要部署sshpass、scp
本机需要python3.4及以上,以及安装paramiko库
各源主机及目的主机能互联互通,ssh安全连接过
''' # 默认运行配置:源
example_src = {
"ip": "10.112.98.132",
"port": 22,
"username": "root",
"password": "111",
"file": "/root/111.o"
} # 默认运行配置:目的,数组
example_dst = [
{
"ip": "10.112.98.134",
"port": 22,
"username": "root",
"password": "222",
"file": "/root/222.o"
},
{
"ip": "10.112.98.151",
"port": 22,
"username": "root",
"password": "333",
"file": "/root/333.o"
},
{
"ip": "10.112.96.67",
"port": 22,
"username": "root",
"password": "444",
"file": "/root/111.o"
}
] # 显示详细信息
# detail = False
detail = True def do_copy(src_host, src_ssh, src_md5, dst_host, srcfile, compress=True, force=False, packname=".tarxjb"):
print()
print("*****cp*****")
print()
dst_ssh = create_ssh(dst_host)
# dst_ssh.exec_command('ssh -o "StrictHostKeyChecking no" {name}@{ip}'.format(name=src_host["username"],ip=src_host["ip"]))
# 检查对方是否已存在相同名字文件
if check_exist(dst_ssh, dst_host["file"]):
# 存在相同,判断是否强制覆盖,不覆盖则自动备份
if detail:
print("res:exist a same name file")
if not force:
if detail:
print("res:do backup")
backup_file_cmd = "mv {file} {file}.x.backup".format(
file=dst_host["file"])
dst_ssh.exec_command(backup_file_cmd)
# 传输文件指令
scp_cmd = "sshpass -p {pw} scp -P {port} {sfile} {duser}@{dip}:{dfile}".format(
port=dst_host["port"], pw=dst_host["password"], sfile=srcfile+packname, duser=dst_host["username"], dip=dst_host["ip"], dfile=srcfile+packname)
sin, sout, serr = src_ssh.exec_command(scp_cmd)
if detail:
print(sout.read().decode())
else:
sout.read().decode()
if detail:
print("res:scp done")
dst_md5 = compu_md5(dst_ssh, srcfile+packname)
if dst_md5 is None:
# md5计算失败
if detail:
print("err:md5 failed")
del_pack(dst_ssh, srcfile+packname)
return False
if src_md5 != dst_md5:
# md5不同,需要撤回包
if detail:
print("err:md5 is not consist")
del_pack(dst_ssh, srcfile+packname)
return False
# input("ddd")
res = compu_tar(dst_ssh, file=srcfile, type="x",
compress=compress, packname=packname)
# input("ddd")
if detail:
print("res:dst tar success")
if not res:
# 解压失败,需要撤回包
if detail:
print("err:tar failed")
del_pack(dst_ssh, srcfile+packname)
return False
if detail:
print("res:file check success")
# 删除中间包
# input("ddd")
del_pack(dst_ssh, srcfile+packname)
if detail:
print("res:remove dst temp pack success")
# 修改文件名
# input("ddd")
mv_file_cmd = "mv {file1} {file2}".format(
file1=srcfile, file2=dst_host["file"])
dst_ssh.exec_command(mv_file_cmd)
if detail:
print("res:filename update success")
dst_ssh.close()
if detail:
print("ok: {file} done".format(file=dst_host["file"]))
return True def copy(src_host, dst, compress=True, force=False):
# 复制文件,一对多
# 获取于源ip的ssh链接
src_ssh = create_ssh(src_host)
# 确定压缩参数
gzip = ""
if compress:
gzip = "z"
# 判断文件是否存在
res = check_exist(src_ssh, src_host["file"])
if detail:
print("src file exist? "+str(res))
# 执行压缩、打包命令
packname = "." + str(int(time.time())) + ".tarxjb"
res = compu_tar(src_ssh, src_host["file"],
"c", compress=compress, packname=packname)
if detail:
print("res:tar success? "+str(res))
# 计算原文件md5值
src_md5 = compu_md5(src_ssh, src_host["file"]+packname)
if detail:
print("src md5 :"+str(src_md5))
# 执行复制操作
res_cp = []
for row in dst:
res_cp.append({row["file"]: do_copy(
src_host, src_ssh, srcfile=src_host["file"], src_md5=src_md5, dst_host=row, packname=packname)})
# 删除本地临时文件
# input("ddd")
del_pack(src_ssh, src_host["file"]+packname)
if detail:
print("res:remove src temp pack success")
print()
print("*****res*****")
for index in res_cp:
print(index)
print("*****res*****")
print()
print("ok: all done!")
src_ssh.close() def del_pack(ssh, file):
# 删除中间包
del_cmd = "rm -f {file}".format(file=file)
sin, sout, serr = ssh.exec_command(del_cmd)
if detail:
print("cmd: rm "+sout.read().decode()) def create_ssh(host):
# 创建ssh链接
ssh = pm.SSHClient()
ssh.set_missing_host_key_policy(pm.AutoAddPolicy())
ssh.connect(hostname=host["ip"], port=host["port"],
username=host["username"], password=host["password"])
return ssh def compu_tar(ssh, file, type="c", compress=True, packname=".tarxjb"):
# 打包及解包
# gzip表示是否要用gzip压缩,默认压缩
gzip = ""
if compress:
gzip = "z"
# 打包指令
tar_cmd = "tar c{gzip}vfpP {file}{packname} {file} 2>/dev/null".format(
gzip=gzip, file=file, packname=packname)
# 解包指令
if type == "x":
tar_cmd = "tar x{gzip}vfpP {file}{packname} 2>/dev/null".format(
gzip=gzip, file=file, packname=packname)
if detail:
print("cmd: tar: " + tar_cmd)
stdin, stdout, stderr = ssh.exec_command(tar_cmd)
res = stdout.read().decode()
if res == "":
if detail:
print("err: {file}{packname} tar failed".format(
file=file, packname=packname))
return False
return True def check_exist(ssh, file):
# 检查文件是否存在
exist_cmd = "find {file} 2>/dev/null".format(file=file)
stdin, stdout, stderr = ssh.exec_command(exist_cmd)
res = stdout.read().decode()
if res == "":
if detail:
print("res: {file} is not exist".format(file=file))
return False
else:
if detail:
print("res: {file} is exist".format(file=file))
return True def compu_md5(ssh, file):
# 计算文件md5
src_md5_cmd = "md5sum {file} 2>/dev/null | awk \'{{print $1}}\'".format(
file=file)
stdin, stdout, stderr = ssh.exec_command(src_md5_cmd)
src_md5 = stdout.read().decode()
if detail:
print("cmd: md5 :"+src_md5_cmd)
print("res: cmd md5 res : "+src_md5)
if src_md5 == "":
if detail:
print("err: {file} md5 or tar failed".format(file=file))
src_md5 = None
else:
if detail:
print("res: {file} md5 : {md5}".format(file=file, md5=src_md5))
return src_md5 if __name__ == "__main__":
print("easy tar tool")
copy(example_src, example_dst, compress=True)

easy-copy服务器文件拷贝简易小工具的更多相关文章

  1. scp文件拷贝简易使用

    scp远程复制 属性变化 需要复制所属关系需要用-p选项 源目录复制之后目的目录的属性: srcdrwxr-xr-x. 2 root root 6 9月 4 16:28 2.txt dstdrwxr- ...

  2. BAT实现服务器文件同步

    服务器文件同步有很多工具,例如 GoodSync.rsync.BitTorrent Sync等……其实WINDOWS下自带了一个文件同步利器:ROBOCOPY.它是一个命令行的目录复制命令,自从Win ...

  3. web:频繁刷新浏览器的页面【小工具】

    [目的] 频繁刷新某一浏览器页面,小测试一下加载性能,或者打开的文件是否及时关闭,会不会导致服务器奔溃 [小工具] 新建txt,输入以下内容,并保存为html的格式,然后在浏览器中打开,则会定时刷新指 ...

  4. Python趣味实用小工具

    代码地址如下:http://www.demodashi.com/demo/12918.html python 趣味实用小工具 概述 用python实现的三个趣味实用小工具: 图片转Execl工具 , ...

  5. wordpress主题之后台菜单编辑,小工具

    1一:菜单编辑 在functions.php 文件加入 if (function_exists('register_nav_menus')) { register_nav_menus(array( / ...

  6. WPF做的迁移文件小工具

    客户这边需要往服务器上传PDF文件.然后PDF文件很多,需要挑出来的PDF文件也不少.因此做了个小工具. 功能很简单,选定源文件夹,选定记录着要提取的文件的excel 文件.OK ,界面如下. XAM ...

  7. 自己动手写一个U盘拷贝小工具

    这是五一期间,参照知乎上一篇的文章<十行代码--用python写一个USB病毒>写成的,最初只是单纯的想写成死循环,直到文件占满硬盘为止,第一个遇到的问题是,拷贝到硬盘之后,由于要无限次拷 ...

  8. 两台linux服务器相互拷贝文件的两个方法

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...

  9. WPF根据Oracle数据库的表,生成CS文件小工具

    开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...

随机推荐

  1. ScratchJr是什么,有什么作用

    什么是ScratchJr? ScratchJr是一个入门级的编程语言,可以让5到7岁的小朋友去创建他们的互动故事和游戏.孩子们使用图形化的程序积木让角色移动.跳跃.舞蹈.唱歌.孩子们可以利用绘图编辑器 ...

  2. 智能指针类模板(上)——STL中的智能指针

    智能指针类模板智能指针本质上就是一个对象,它可以像原生指针那样来使用. 智能指针的意义-现代C++开发库中最重要的类模板之一-C++中自动内存管理的主要手段-能够在很大程度上避开内存相关的问题 1.内 ...

  3. 多线程(六)多线程同步_SemaPhore信号量

    信号量依然是一种内核同步对象,它的作用在于控制共享资源的最大访问数量 例如:我们有一个服务器,为这服务器创建一个线程池,线程池有五个线程,每个线程处理1个请求.当五个线程都在处理请求时,这个线程池己到 ...

  4. boostrap --- 弹窗modal的使用

    <input id="btntext" type="button" value="添加文本组件" data-toggle=" ...

  5. 设计模式-单例模式(Singleton) (创建型模式)

    //以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Singleton.h #pragma once #include<iostream> class Sin ...

  6. 【Excel】创建目录

    1.=INDEX(GET.WORKBOOK(1),ROW(A1))&T(NOW()) 2. =IFERROR(HYPERLINK(我的目录&"!A1",MID(我的 ...

  7. pytest--生成HTML报告

    前戏 我们做自动化,测试报告是必不可少的.方便自己查看,也可以供领导查看,一个测试报告就可以说明我们做了哪些事情,pytest-html插件给我们提供了一个很简陋的测试报告,为什么说简陋,因为是真简陋 ...

  8. vue.js 使用v-model v-once

    v-model 双向绑定 v-once 单项绑定 代码: <!doctype html> <html lang="en"> <head> < ...

  9. 【2019.7.20 NOIP模拟赛 T1】A(A)(暴搜)

    打表+暴搜 这道题目,显然是需要打表的,不过打表的方式可以有很多. 我是打了两个表,分别表示每个数字所需的火柴棒根数以及从一个数字到另一个数字,除了需要去除或加入的火柴棒外,至少需要几根火柴棒. 然后 ...

  10. 行为驱动:Cucumber + Java - 实现数据的参数化

    1.什么是参数化 实际设计测试用例过程中,我们经常会用等价类.边界值这样的方法,针对一个功能进行测试数据上的测试,比如一个输入框,正向数据.逆向数据,非法输入等等 2.Cucumber的数据驱动 同上 ...