此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:
-
-
-
- import os,sys,time,socket,paramiko,MySQLdb
-
- def mysql_connect(ip,sql):
- try:
- conn = MySQLdb.connect(host = ip,user = 'repl',passwd = 'VQrtetr8',connect_timeout=5)
- cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
- cursor.execute(sql)
- alldata = cursor.fetchall()
- return alldata
- except MySQLdb.Error,e:
- return e.args[0]
-
- def ssh_conm(conm):
- host = mysql_connect('127.0.0.1',"show slave status")[0]["Master_Host"]
-
- username='root'
- password = '3141315'
- port = 63008
- s=paramiko.SSHClient()
- s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- s.connect(host,port,username,password)
- stdin,stdout,stderr=s.exec_command(conm)
- result = stdout.read()
- s.close()
- return result
-
- def log_pos():
- result = mysql_connect('127.0.0.1',"show slave status")
- host = result[0]["Master_Host"]
- s_log = result[0]["Relay_Master_Log_File"].split('.')[1]
- s_pos = result[0]["Exec_master_log_pos"]
- result = mysql_connect(host,"show master status")
- if result == 2003:
- choose = raw_input('Can not connect Master,Do you go on ? (y or n) :')
- if choose == 'y':
- return 0,0,0,0
- else:
- sys.exit(1)
- m_log = result[0]["File"].split('.')[1]
- m_pos = result[0]["Position"]
- return s_log,m_log,s_pos,m_pos
-
- def show():
- print "Check Master-Slave now..."
- for i in range(5):
- s_log,m_log,s_pos,m_pos = log_pos()
- if s_log == m_log and s_pos == m_pos:
- print "Slave and Master is OK."
- break
- else:
- print "Slave and Master not OK,please wait moment."
- if i == 4:
-
- choose = raw_input('Master-slave not synchronous,Do you go on ? (y or n) :')
- if choose == 'y':
- break
- else:
- sys.exit(1)
- i = i + 1
- time.sleep(5)
-
- def replace():
-
- print "Before switching IP:"
- s_ip = os.popen("cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep IPADDR|awk -F '=' '{print $2}'").read().split()[:1][0]
- print "s_ip: " + s_ip
- conm = "cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep IPADDR|awk -F '=' '{print $2}'"
- m_ip = ssh_conm(conm).split()[:1][0]
- print "m_ip: " + m_ip
- print
-
- conm = "ifdown eth0"
- ssh_conm(conm)
-
- show()
-
- os.popen("sed -i 's/%s/%s/' /etc/sysconfig/network-scripts/ifcfg-eth0" % (s_ip,m_ip))
- print "Slave change IP is OK"
-
- conm = "sed -i 's/%s/%s/' /etc/sysconfig/network-scripts/ifcfg-eth0" % (m_ip,s_ip)
- ssh_conm(conm)
- print "Master change IP is OK"
-
- print
- print "After switching IP:"
- s_ip = os.popen("cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep IPADDR|awk -F '=' '{print $2}'").read().split()[:1][0]
- print "s_ip: " + s_ip
- conm = "cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep IPADDR|awk -F '=' '{print $2}'"
- m_ip = ssh_conm(conm).split()[:1][0]
- print "m_ip: " + m_ip
-
- mysql_connect("127.0.0.1","slave stop")
- print "Slave stop OK"
-
- print
- print "Network service restart now,please wait ......"
- conm = "ifup eth0"
- ssh_conm(conm)
- os.system("ifdown eth0")
- os.system("ifup eth0")
- print "Network reatrt OK !"
-
- if __name__ == "__main__":
- replace()