This script feature is mainly to find all the files/data1 to/data11 below the current time of day, and copy them to another new machine.
Note: Focus on automatic interactive input password, remote file copy function
#!/usr/bin/pyth#coding=utf-8 #字符编码import os #os模块import pexpect # Automatic interaction Module import time #导入时间模块dir_name =os.popen ("ls -l / |grep ' data[0-9]\{1,2\} ' |awk ' {print $NF} '). ReadLines () #统计/data, followed by a directory with numbers, example/data1-11def cp_file (): # SCP Copy file Functions global jindu #定义全局变量, jindu file_order= ' Scp -ostricthostkeychecking=no %s %[email protected]%s:%s ' % (fullname,user,add_ip,dirname) #执行scp复制命令 # -ostricthostkeychecking=no ", used to avoid Enter the first time execute ssh required revenue yes/no scp_file=pexpect.spawn (file_order,timeout=100000) #执行pexpect的spawn方法 scp_file.expect (' Password: ') #自动交互匹配password关键字 scp_file.sendline (passWord) #自动匹配成功后, Password send scp_file.read () #这个参数我在百度查询的 If the copy is not successful jindu=scp_file.before.split () [ -4] # Determine if the copied file is successful, according to the progress of the adjustment, if the progress of 100%, indicating success. scp_file.close () #进行关闭for dir_line in dir_name: #对dir_name进行匹配 mulu=dir_line.strip () # Remove the spaces on both sides to assign a value find_file= "Find /%s -type f -mtime +1|xargs ls -l " %mulu #判断当前时间1天以外的所有数据 return_file=os.popen (find_file). ReadLines () #执行shell程序, and assign the result to the Retrun_file variable mulu_list=[] #存放目录的列表 for file_line in return_file: #对第一个目录进行循环 fullname=file_line.strip (). Split () [8] #取出文件全路径名 dirname=os.path.dirname (File_line.strip ()). Split () [8] # Remove directory name scp_file_success=file ('/tmp/scp_file_success.log ', ' a ') #同步复制成功写入日志 scp_file_fail=file ('/tmp/scp_file_fail.log ', ' A ') #同步复制失败写入日志 Now_time=time.strftime ('%y%m%d %h:%m:%s ', Time.localtime (Time.time ())) #复制文件当前时间 log_success= '%s %s file sync succeeded!\n ' % (now_time,fullname) #成功日志显示 log_fail= '%s %s file sync failed!\n ' % (now_time,fullname) #失败日志显示 local_fail= '%s %s local file does not exist!\n ' % (now_time,fullname) #检查本地文件是否存在 add_ip= "192.168.3.1 " #新cache节点ip user=" Root " #新cache节点用户 password= "123" #新cache节点密码 if Os.path.isfile (FullName) ==true: #判断旧节点拷贝文件是否存在, if present, continue execution if there is no write logging, continue looping if dirname not in mulu_list: #判断目录是否存在mulu_list列表, if not present, create a directory to the new node, and then copy the file mkdir_order= ' mkdir -p %s ' %dirname #新建目录用到的权限 ssh_server= "ssh -ostricthostkeychecking=no %s '%s '" % (Add_ip,mkdir_order ) #执行ssh登陆脚本 create_ Directory=pexpect.spawn (ssh_server) #执行pexpect的spawn方法, SSH login create_directory.expect (' Password: ') #自动交互匹配password关键字 create_directory.sendline (password) #自动匹配成功后, passwordSend create_directory.read () #这个参数我在百度查询的, if you do not add the copy is unsuccessful create_directory.close () #进行关闭 mulu_list.append (dirname) #创建成功的目录添加到列表 cp_file () #执行文件复制方法 if jindu== "100%": #判断复制文件是否成功, according to the progress of the adjustment, if the progress of 100%, indicating success . scp_file_success.write (log_success) else: scp_file_fail.write (Log_fail) else: #v如果新节点目录存在则直接拷贝文件 cp_file () #执行文件复制方法 if jindu== "100%": #判断复制文件是否成功, according to the progress of the adjustment, if the progress of 100%, indicating success . scp_file_success.write (log_success) else: scp_file_fail.write (Log_ Fail) else: scp_file_fail.write(local_fail) del mulu_list[:] #每次循环一个data目录就清空列表scp_file_ Success.close () #日志关闭scp_file_fail. Close () #日志关闭
This article is from the "Chengdu @ Ah-like" blog, please be sure to keep this source http://azhuang.blog.51cto.com/9176790/1606159
Automate interactive input of passwords for remote file copying