全部博文(298)
分类: Python/Ruby
2012-04-23 16:33:40
【rsync】向多台服务器传输文件的脚本
整理自:=
测试环境:python 2.4
/2.6
使用:
1.运行的时候需要输入密码(第一次随便输入,只是要生成list文件),打印出待执行的命令.
2.生成qrsync.list配置文件.在文件里定义用户名,服务器列表,要传输的文件和远程目录
3.主要调用了rsync命令
4.输入yes后,才调用脚本中的rsync_core函数执行传输.
5. 主机检测函数 socket_host,避免定义了不存在的主机,或者主机名错误,导致脚本退出.
脚本内容如下:
1. #!/usr/bin/env python 2. #-*- coding: UTF-8 -*- 3. # author: 4. # changetime: 20120329 5. 6. import pexpect 7. import getpass, os 8. import sys, getopt 9. import socket 10. import traceback 11. from ConfigParser import ConfigParser 12. 13. def usage(): 14. print '\nUsage: qrsync.py ' 15. print '编辑.list配置文件' 16. sys.exit(0) 17. 18. def rsync_input(): 19. try: 20. #files = raw_input('Flies: ') 21. #hosts = raw_input('Hostname: ') 22. #hosts = 'hostname' 23. #user = raw_input('User: ') 24. #user = 'lianjie.ning' 25. password = getpass.getpass('Please enter password: ') 26. except: 27. print '\nERROR: input error,please try agent run script\n' 28. usage() 29. return password 30. 31. def load_config(): 32. cfg_path = os.path.join(os.environ["HOME"], "bin/qrsync.list") 33. cfg_temp = '''[connects] 34. user = 35. hosts = 36. # 跳板机存放authorized_keys文件,用ssh登录仍然需要输入token密码 37. # 但是应用rsync命令,则可以直接传送. 38. files = 39. rpath = 40. ''' 41. #将cfg_temp写入文件 42. # user 用户 43. # hosts 将要传输文件的远程主机列表 44. # files 需要传输的文件 45. # rpath 远程主机的目录,传输的用户要有写入权限. 46. if not os.path.exists(cfg_path): 47. open(cfg_path, 'w').write(cfg_temp) 48. #加载stat模块,修改文件属性. 49. #os.chmod(cfg_path, stat.S_IREAD) 50. cfg = ConfigParser() 51. cfg.read(cfg_path) 52. user = cfg.get('connects', 'user') 53. hosts = cfg.get('connects', 'hosts') 54. files = cfg.get('connects', 'files') 55. rpath = cfg.get('connects', 'rpath') 56. return (user,hosts,files,rpath) 57. 58. # 检测列表中的主机 59. def socket_host(host): 60. try: 61. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 62. sock.settimeout(3) 63. sock.connect((host, 22)) 64. sock.close() 65. return 0 66. except: 67. print 'ping: unknown host %s' % (host) 68. return 1 69. #sys.exit(0) 70. 71. def rsync_core(user, host, files, rpath, password): 72. #password = rsync_input() 73. #print command 74. #print 'exec command: /usr/bin/rsync -tvzrpc %s %s@hostname:.ssh/' % (files, user) 75. #print host 76. #check = raw_input('Are you sure transmitted to the tools(yes/no): ') 77. check = 'yes' 78. if check == 'yes': 79. rsync_newkey = 'Are you sure you want to continue connecting' 80. child = pexpect.spawn('/usr/bin/rsync -tvzrpc %s %s@%s:%s' % (files, user, host, rpath)) 81. try: 82. i = child.expect([pexpect.TIMEOUT, rsync_newkey, 'password: ', "id_rsa': "]) 83. except: 84. print '出现问题的原因:' 85. print '1.该主机不存在' 86. print '2.命令不需要交互,请检查主机直接是否做了授信' 87. print 'please checkout host and host...' 88. sys.exit(0) 89. if i == 0: # Timeout 90. print 'ERROR!' 91. print child.before, child.after 92. return None 93. if i == 1: # SSH does not have the public key. Just accept it. 94. child.sendline ('yes') 95. child.expect ('password: ') 96. i = child.expect([pexpect.TIMEOUT, 'password: ']) 97. if i == 0: # Timeout 98. print 'ERROR!' 99. print child.before, child.after 100. return None 101. child.sendline(password) 102. child.expect(pexpect.EOF) 103. print child.before # Print the result of the command 104. print child.after 105. else: 106. sys.exit(0) 107. 108. def main(): 109. # 读取密码文件 110. password = rsync_input() 111. # load file 112. (user,hosts,files,rpath) = load_config() 113. if user == None: 114. sys.exit(0) 115. # print user, password 116. filter_hosts = [] 117. hosts = hosts.strip().split(' ') 118. for host in hosts: 119. print host 120. i = socket_host(host) 121. if i == 0: 122. #print '111' 123. filter_hosts.append(host) 124. else: 125. #print '222' 126. pass 127. #for i in filter_hosts: 128. #print i 129. #sys.exit(0) 130. for i,host in enumerate(filter_hosts): 131. print i,"rsync -tvzrp %s %s@%s:%s" % (files,user,host,rpath) 132. #sys.exit(0) 133. 134. #确认 135. check = raw_input('Are you sure transmitted to the host(yes/no): ') 136. if check == 'yes': 137. for i,host in enumerate(hosts): 138. rsync_core(user, host, files, rpath, password) 139. print i,host 140. else: 141. sys.exit(0) 142. 143. if __name__ == '__main__': 144. try: 145. main() 146. except Exception, e: 147. print str(e) 148. traceback.print_exc() 149. os._exit(1)
|