前言:mysql
添加顏色打印功能,實現了鏈接db數據庫登錄系統,查詢,添加,刪除以及登陸成功後執行相關操做系統。sql
''' 用戶登陸-添加-刪除,登錄成功後執行系統命令,失敗提示。經過mysql數據庫認證。 打印color ''' #encoding:utf8 import os import sys import datetime import MySQLdb import time import sh class Colored(object): # 顯示格式: \033[顯示方式;前景色;背景色m # 只寫一個字段表示前景色,背景色默認 RED = '\033[31m' # 紅色 GREEN = '\033[32m' # 綠色 YELLOW = '\033[33m' # ××× BLUE = '\033[34m' # 藍色 FUCHSIA = '\033[35m' # 紫紅色 CYAN = '\033[36m' # 青藍色 WHITE = '\033[37m' # 白色 #: no color RESET = '\033[0m' # 終端默認顏色 def color_str(self, color, s): return '{}{}{}'.format( getattr(self, color), s, self.RESET ) def red(self, s): return self.color_str('RED', s) def green(self, s): return self.color_str('GREEN', s) def yellow(self, s): return self.color_str('YELLOW', s) def blue(self, s): return self.color_str('BLUE', s) def fuchsia(self, s): return self.color_str('FUCHSIA', s) def cyan(self, s): return self.color_str('CYAN', s) def white(self, s): return self.color_str('WHITE', s) class initdb(object): #conn=MySQLdb.connect(host='10.7.7.180',user='root',passwd='123456',db='test') #cur=conn.cursor() def __init__(self,username='NULL',password='NULL'): self.username=username self.password=password self.conn=MySQLdb.connect(host='10.7.7.180',user='root',passwd='123456',db='test') self.cur=self.conn.cursor() def add_user(self): itime=datetime.datetime.now() self.cur.execute("INSERT INTO user (username, password,createtime) VALUES ('%s','%s','%s')" % (self.username,self.password,itime)) self.cur.close() self.conn.commit() self.conn.close() return "Add user %s successful!" % self.username def login_check(self): self.cur.execute("select * from user where username='%s' and password = '%s'" % (self.username,self.password)) qur_result = self.cur.fetchall() if qur_result == (): return 0 else: return 1 self.cur.close() self.conn.close() def del_user(self): self.cur.execute("delete from user where username='%s'" % self.username) self.cur.close() self.conn.commit() self.conn.close() def see_user(self): self.cur.execute("select * from user") data = self.cur.fetchall() self.cur.close() self.conn.close() d = list(data) for x in d: print x while True: color = Colored() print color.fuchsia('*'*50) choice=raw_input(color.green("請選擇操做選項:\n\t[1]添加用戶\n\t[2]登錄用戶\n\t[3]刪除用戶\n\t[4]查看用戶信息\n\t[5]退出程序\n請選擇:")) if choice == '1': inname=raw_input(color.yellow("請輸入要添加的用戶名稱:")) inpassword=raw_input(color.yellow("請輸入要爲%s添加的密碼:" % inname)) initdb(inname,inpassword).add_user() print color.blue("用戶%s添加成功!" % inname) elif choice == '2': username=raw_input(color.yellow("請輸入用戶名:")) password=raw_input(color.yellow("請輸入密碼:")) f=initdb(username,password).login_check() if f == 0: print color.red("Login faild!please check your username and password!") elif f == 1: print color.green("Login successful!\nHello,MR %s" % username) time.sleep(3) filelist=open('/etc/passwd') for i in filelist.readlines(): print i time.sleep(3) print sh.ifconfig('ens33') print sh.df('-h') print sh.free('-m') print sh.tail('-100','/var/log/auth.log') else: print "something wrong!" elif choice == '3': dname=raw_input(color.yellow("請輸入要刪除的用戶名稱:")) initdb(dname).del_user() print color.red("用戶%s刪除成功!" % dname) elif choice == '4': initdb().see_user() elif choice == '5': print color.green("程序已退出,歡迎下次光臨!") break else: print color.red("未知的選項,請從新輸入!")