前言:DBA日常运维除了通过自动化平台来进行日常操作以外,免不了还是会登陆到实例上进行操作,今天花了点时间写了个极其简易的数据库跳板机。

数据库

  1. #建库:
  2. create database jumpmysql
  3. #建表:
  4. CREATE TABLE `mysql_server` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `tag` varchar(50) DEFAULT NULL,
  7. `ip` varchar(15) DEFAULT NULL,
  8. `port` int(11) DEFAULT NULL,
  9. `soketdir` varchar(50) DEFAULT NULL,
  10. `datadir` varchar(50) DEFAULT NULL,
  11. `mysql_user` varchar(20) DEFAULT NULL,
  12. `mysql_pass` varchar(50) DEFAULT NULL,
  13. `status` int(11) DEFAULT NULL,
  14. `info` varchar(500) DEFAULT NULL,
  15. PRIMARY KEY (`id`)
  16. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
  17. 1 row in set (0.00 sec)
  18. #插入测试数据:
  19. insert into mysql_server values(1,'xxx主库','10.100.xx.xxx',3306,'/storage/mysql/mysql.sock','/storage/mysql/data/','xxx','123456',1,'xxx主库');
  20. insert into mysql_server values(2,'xxx备库','10.100.xx.xxx',3306,'/storage/mysql/mysql.sock','/storage/mysql/data/','xxx','123456',1,'xxx备库');

python脚本

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import os
  4. import MySQLdb as mdb
  5. from passde import encrypt, decrypt
  6. conn = mdb.connect(host='127.0.0.1', port=3306, user='test', passwd=decrypt('e51bc8b1c7f3810573d0fb9d7c2f8c01'), db='jumpmysql', charset='utf8')
  7. while True :
  8. cursor = conn.cursor()
  9. cursor.execute("SELECT * FROM mysql_server")
  10. servers = cursor.fetchall()
  11. for server in servers:
  12. id = server[0]
  13. ip = server[2]
  14. port = server[3]
  15. tag = server[1]
  16. # 打印结果
  17. print "\033[1;33;44m %d \033[0m: %s %d %s" % \
  18. (id, ip, port, tag)
  19. serverid = raw_input("请选择实例ID:")
  20. #获取连接信息
  21. SQL = "SELECT ip,port,mysql_user,mysql_pass FROM mysql_server where id= {SERVER_ID}".format(SERVER_ID=serverid)
  22. cursor.execute(SQL)
  23. data = cursor.fetchone()
  24. host = data[0]
  25. port = data[1]
  26. user = data[2]
  27. password = decrypt(str(data[3]))
  28. connectinfo = "mysql -h %s -P%d -u%s -p%s" % (host, port, user, password)
  29. try:
  30. os.system(connectinfo)
  31. except:
  32. print "信息错误,请重试!

加密解密脚本

  1. [root@localhost src]# cat passde.py
  2. # -*- coding:utf-8 -*-
  3. #!/usr/bin/env python
  4. from Crypto.Cipher import AES
  5. from binascii import b2a_hex,a2b_hex
  6. # from deveops.settings import SECRET_KEY
  7. SECRET_KEY = '1x$!#dwp2_6^tdgs1nv8pwgutbc#4m%#qaz!m!0h_f*%6fp+vt'
  8. KEY = SECRET_KEY
  9. KEY_LENGTH=16
  10. def encrypt(text):
  11. # 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
  12. cryptor = AES.new(KEY[0:KEY_LENGTH], AES.MODE_CBC, KEY[0:KEY_LENGTH])
  13. length = KEY_LENGTH
  14. count = len(text)
  15. add = length - (count % length)
  16. text = text + ('\0' * add)
  17. ciphertext = cryptor.encrypt(text)
  18. # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
  19. # 所以这里统一把加密后的字符串转化为16进制字符串
  20. return b2a_hex(ciphertext)
  21. def decrypt(text):
  22. cryptor = AES.new(KEY[0:16], AES.MODE_CBC, KEY[0:16])
  23. plain_text = cryptor.decrypt(a2b_hex(text))
  24. return plain_text.rstrip('\0')
  25. if __name__ == '__main__':
  26. e = encrypt("123456")
  27. d = decrypt('e51bc8b1c7f3810573d0fb9d7c2f8c01')
  28. print e, d

测试效果
运行脚本
【python】一个极其简易的数据库跳板机 - 图2
效果还不错