1 # -*- coding:utf-8 -*-
2 import paramiko,os,sys,time
3 #-----------------------更新svn-------------------------------------------------
4 msg = "正在更新本地svn"
5 msg1 = "更新完成"
6 msg9 = "正在执行sql脚本"
7 print msg.center(50,"*")
8 os.system("cd /data/yunweisvn/sh/clearscript && svn update")
9 print msg1.center(50,"*")
10 #--------------------拷贝清库脚本到目标机器-------------------------------------
11
12 host_name = sys.argv[1]
13 password = "B^Dc%4LSBvhZZK3B"
14 port = 22
15 cmd = "/data/sh/clear.sh"
16 local_sunif = "/data/yunweisvn/sh/clearscript/sunif/cleartable.sql" #运营
17 remote_sunif = "/data/cleardb/sunif/initscrpit/cleartable.sql" #运营
18 local_sunfcbp = "/data/yunweisvn/sh/clearscript/sunfcbp/cleartable.sql" #前置
19 remote_sunfcbp = "/data/cleardb/sunfcbp/clear.sql" #前置
20 local_ltts = "/data/yunweisvn/sh/clearscript/ltts/scripts/cleartable.sql" #核心
21 remote_ltts = "/data/cleardb/cbmain/scripts/cleartable.sql" #核心
22
23
24 def ssh_scp_put(ip,port,user,password,local_file,remote_file):
25 ssh = paramiko.SSHClient()
26 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
27 ssh.connect(ip, 22, 'root', password)
28 a = ssh.exec_command('date')
29 stdin, stdout, stderr = a
30 print stdout.read()
31 sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
32 sftp = ssh.open_sftp()
33 sftp.put(local_file, remote_file)
34
35
36 def ssh_cmd(ip,port,cmd,user,passwd):
37 result = ""
38 try:
39 ssh = paramiko.SSHClient()
40 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
41 ssh.connect(ip,port,user,passwd)
42 stdin, stdout, stderr = ssh.exec_command(cmd)
43 result = stdout.read()
44 print result
45 ssh.close()
46 except:
47 print "ssh_cmd err."
48 return result
49
50
51
52 mag2 = "正在复制sql脚本到目标机器"
53 print mag2.center(50,"*")
54 if host_name == "s3":
55 ip = "10.10.123.96"
56 ssh_scp_put(ip,port,"root",password,local_sunif,remote_sunif)
57 ssh_scp_put(ip, port, "root", password, local_sunfcbp, remote_sunfcbp)
58 ssh_scp_put(ip, port, "root", password, local_ltts, remote_ltts)
59 print msg9.center(50,"*")
60 ssh_cmd(ip,port,"sh /mnt/clear.sh","root",password)
61
62 elif host_name == "qa1":
63 ip = "10.10.105.91"
64 ssh_scp_put(ip,port,"root",password,local_sunif,remote_sunif)
65 ssh_scp_put(ip, port, "root", password, local_sunfcbp, remote_sunfcbp)
66 ssh_scp_put(ip, port, "root", password, local_ltts, remote_ltts)
67 print msg9.center(50,"*")
68 ssh_cmd(ip,port,cmd,"root",password)
69
70 elif host_name == "qa2":
71 ip = "10.10.92.48"
72 ssh_scp_put(ip,port,"root",password,local_sunif,remote_sunif)
73 ssh_scp_put(ip, port, "root", password, local_sunfcbp, remote_sunfcbp)
74 ssh_scp_put(ip, port, "root", password, local_ltts, remote_ltts)
75 print msg9.center(50,"*")
76 ssh_cmd(ip,port,cmd,"root",password)
77
78 elif host_name == "qa3":
79 ip = "10.10.50.30"
80 ssh_scp_put(ip,port,"root",password,local_sunif,remote_sunif)
81 ssh_scp_put(ip, port, "root", password, local_sunfcbp, remote_sunfcbp)
82 ssh_scp_put(ip, port, "root", password, local_ltts, remote_ltts)
83 print msg9.center(50,"*")
84 ssh_cmd(ip,port,cmd,"root",password)
85
86 elif host_name == "qa4":
87 ip = "10.10.40.136"
88 ssh_scp_put(ip,port,"root",password,local_sunif,remote_sunif)
89 ssh_scp_put(ip, port, "root", password, local_sunfcbp, remote_sunfcbp)
90 ssh_scp_put(ip, port, "root", password, local_ltts, remote_ltts)
91 print msg9.center(50,"*")
92 ssh_cmd(ip,port,cmd,"root",password)
93
94 elif host_name == "qa5":
95 ip = "10.10.189.139"
96 ssh_scp_put(ip,port,"root",password,local_sunif,remote_sunif)
97 ssh_scp_put(ip, port, "root", password, local_sunfcbp, remote_sunfcbp)
98 ssh_scp_put(ip, port, "root", password, local_ltts, remote_ltts)
99 print msg9.center(50,"*")
100 ssh_cmd(ip,port,cmd,"root",password)