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服务器文件拷贝简易小工具的更多相关文章
- scp文件拷贝简易使用
scp远程复制 属性变化 需要复制所属关系需要用-p选项 源目录复制之后目的目录的属性: srcdrwxr-xr-x. 2 root root 6 9月 4 16:28 2.txt dstdrwxr- ...
- BAT实现服务器文件同步
服务器文件同步有很多工具,例如 GoodSync.rsync.BitTorrent Sync等……其实WINDOWS下自带了一个文件同步利器:ROBOCOPY.它是一个命令行的目录复制命令,自从Win ...
- web:频繁刷新浏览器的页面【小工具】
[目的] 频繁刷新某一浏览器页面,小测试一下加载性能,或者打开的文件是否及时关闭,会不会导致服务器奔溃 [小工具] 新建txt,输入以下内容,并保存为html的格式,然后在浏览器中打开,则会定时刷新指 ...
- Python趣味实用小工具
代码地址如下:http://www.demodashi.com/demo/12918.html python 趣味实用小工具 概述 用python实现的三个趣味实用小工具: 图片转Execl工具 , ...
- wordpress主题之后台菜单编辑,小工具
1一:菜单编辑 在functions.php 文件加入 if (function_exists('register_nav_menus')) { register_nav_menus(array( / ...
- WPF做的迁移文件小工具
客户这边需要往服务器上传PDF文件.然后PDF文件很多,需要挑出来的PDF文件也不少.因此做了个小工具. 功能很简单,选定源文件夹,选定记录着要提取的文件的excel 文件.OK ,界面如下. XAM ...
- 自己动手写一个U盘拷贝小工具
这是五一期间,参照知乎上一篇的文章<十行代码--用python写一个USB病毒>写成的,最初只是单纯的想写成死循环,直到文件占满硬盘为止,第一个遇到的问题是,拷贝到硬盘之后,由于要无限次拷 ...
- 两台linux服务器相互拷贝文件的两个方法
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- WPF根据Oracle数据库的表,生成CS文件小工具
开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...
随机推荐
- 201871010111-刘佳华《面向对象程序设计(java)》第6-7周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第6-7周学习总结 实验六 继承定义与使用 实验时间 2019-9-29 第一部分:理论部分. 1.继承:已有类来构建新类的 ...
- Vue+cordova开发App
Vue+cordova开发App https://www.imooc.com/article/70062
- C++ int 和string互相转化
1.int转换成string );//"-12" );//"12" +);//"1" 2.string转换成int );//"-1 ...
- MySQL学习笔记1——DDL
DDL 1.数据库 *查看所有数据库:SHOW DATABASES;*切换(选择要操作的)数据库:USE 数据库名;*创建数据库:CREATE DATABASES [IF NOT EXISTS] my ...
- LG3119 「USACO2015JAN」Grass Cownoisseur
问题描述 LG3119 题解 显然,如果有个环,一定是全部走完的. 所以缩点,缩出一个 \(\mathrm{DAG}\) . 只能走一次反向,于是在正图和反图上各跑一次,枚举边,取 \(\mathrm ...
- 用arthas查看JVM已加载的类及方法信息
1.sc:“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息,这个命令支持的参数有 [d].[E].[f] 和 [x:]. [d] 输出当前类的详细信 ...
- 洛谷P3157 [CQOI2011]动态逆序对
题目大意: 给定\(1\)到\(n\)的一个排列,按照给定顺序依次删除\(m\)个元素,计算每个元素删除之前整个序列的逆序对数量 基本套路:删边变加边 那么我们不就是求满足\(pos_i<pos ...
- Token认证,如何快速方便获取用户信息
背景 我们有一个Web项目,这个项目提供了很多的Rest API.也做了权限控制,访问API的请求必须要带上事先认证后获取的Token才可以. 认证的话就在Filter中进行的,会获取请求的Token ...
- Flink101-快速示例
验证本文需要具备Docker及Docker-composer,作者使用的环境为Mac + Docker Docker启动Flink集群 首先下载Flink的镜像docker pull flink,我下 ...
- C++:Special Member Functions
Special Member Functions 区别于定义类的行为的普通成员函数,类内有一类特殊的成员函数,它们负责类的构造.拷贝.移动.销毁. 构造函数 构造函数控制对象的初始化过程,具体来说,就 ...