本文共 4918 字,大约阅读时间需要 16 分钟。
Mr. Neo Chen (netkiller), 陈景峰(BG7NYT)
下面是我多年积累下来的经验总结,整理成文档供大家参考:
-
-
-
-
-
-
-
-
-
- import os,subprocess
-
- class Whiptail():
-
- def __init__(self,title = None, backtitle = ""):
- if title :
- self.t = '"'
- if backtitle :
- self.backtitle = '"'
- else:
- backtitle = ""
- self.ismenu = False
- def title(self, tmp):
- self.t = tmp
- return(self)
- def inputbox(self, label, hight, width, init = ""):
- self.form = '--inputbox' + ' "'+label+'" ' + str(hight) + ' ' + str(width) + ' '+init
- return(self)
- def passwordbox(self, label, hight, width):
- self.form = '--passwordbox' + ' "'+label+'" ' + str(hight) + ' ' + str(width)
- return(self)
- def menu(self,lable, item, hight, width, listheight):
- menuitem = ''
- for i in item:
- key,value = i
- menuitem = menuitem + '"'
-
-
-
-
-
-
-
-
-
-
- +tag + '" "' + item + '" "' + status + '" '
- self.radiolist = '--yesno' + ' "'+text+'" ' + str(height) + ' ' + str(width) + ' ' + str(listheight) + ' ' + radioitem
- return(self)
- def run(self):
- screen = None
-
-
- whiptail = 'whiptail ' + ' --title ' + self.t + ' --backtitle ' + self.backtitle +' ' + self.form + ' 3>&2 2>&1 1>&3 | tee /tmp/.whiptail'
-
- try:
-
-
-
-
-
-
-
-
-
- os.system(whiptail)
- f = open('/tmp/.whiptail','r')
- screen = f.read()
- f.close()
- except OSError as e:
- print(e)
-
-
-
- return(screen)
-
- class MySQL():
- mysql='mysql'
- def __init__(self, hosts = None):
- self.dialog = Whiptail('MySQL Adminstrator','MySQL Client')
- self.dbhost = None
- self.dbuser = None
- self.dbpass = None
- if hosts :
- self.hosts = hosts
- def test(self):
- dialog = Whiptail('Test')
- dialog.inputbox('Windows', 10, 20).run()
- def menu(self):
- hosts = []
- hosts.append(('1','MySQL Manager'))
- hosts.append(('2','MySQL Backup'))
- hosts.append(('3','MySQL Restore'))
- m = self.dbhost = self.dialog.menu('Menu', hosts, 15, 30, 3).run()
- return(m)
- def host(self, hosts):
- self.dbhost = self.dialog.menu('Database Host', hosts, 20, 40, 5).run()
- def login(self):
- hight = 8
- width = 40
- self.dbuser = self.dialog.inputbox('User', hight, width, 'root').run()
- self.dbpass = self.dialog.passwordbox('Password', hight, width).run()
- pass
- def dump(self,hosts):
- hight = 8
- width = 40
- self.host(self.hosts)
- self.login()
- dbname = self.dialog.inputbox('Database', hight, width).run()
- dbfile = self.dialog.inputbox('File', hight, width).run()
- yesno = self.dialog.yesno('Backup?', hight, width).run()
- cmd = % (self.dbhost, self.dbuser, self.dbpass, dbname, dbfile)
-
-
-
- os.system(cmd)
- def client(self,hosts):
- hight = 8
- width = 40
- self.host(hosts)
- self.login()
- dbname = self.dialog.inputbox('Database', hight, width).run()
- cmd = % (self.dbhost, self.dbuser, self.dbpass, dbname)
-
- os.system( cmd )
- def restore(self,hosts):
- hight = 8
- width = 40
- self.host(hosts)
- self.login()
- files = os.listdir('/opt/backup')
-
- menu = []
- i = 0
- for f in files:
- menu.append((str(i),f))
- i = i + 1
- dbfile = self.dbhost = self.dialog.menu('Backup History', menu, 20, 30, 7).run()
- dbname = self.dialog.inputbox('Database', hight, width).run()
- cmd = % (backup, self.dbhost, self.dbuser, self.dbpass, dbname)
-
- os.system( cmd )
- def logout(self):
- pass
- def close(self):
- pass
-
- def main(self):
- m = self.menu()
- print(m)
- if m == '2':
- self.dump(self.hosts)
- elif m == '3':
- self.restore(self.hosts)
- else:
- self.client(self.hosts)
-
- if __name__ == '__main__':
- try:
- hosts = []
- hosts.append(('127.0.0.1','localhost'))
- hosts.append(('172.16.0.1','mysql master'))
- hosts.append(('172.16.0.2','mysql slave'))
-
- mysql = MySQL(hosts)
- mysql.main()
- except KeyboardInterrupt:
- print ("Crtl+C Pressed. Shutting down.")
- os.exit()
转载地址:http://wnlxx.baihongyu.com/