mysql查看用戶的權限腳本
1、安裝
mysql支持python包
yum -y install mysql-connector-python
2、查看MySQL 用戶權限的Python腳本
vi py_priv.py
#!/usr/bin/env python
#coding:utf8
#epel源&yum -y install mysql-connector-python
from __future__ import print_function
import re,sys
import mysql.connector as mdb
config = {
'user':'root',
'password':'123456',
'host':'192.168.1.191',
'port':3306,
'database':'mysql'
}
def GetPriv():
show_username="select concat(\"'\",user,\"'\",'@',\"'\",host,\"'\"),password from mysql.user"
cursor.execute(show_username)
users={}
for i in cursor:
username=i[0]
password=i[1]
users[username]=password
for key in users:
try:
show_priv='show grants for %s' % key
cursor.execute(show_priv)
priv=[]
for i in cursor:
priv.append(tuple(re.split(r' TO ',str(*i))[0].split(r' ON ')))
print(''.center(80,'~'))
print('{0:<10} {1}'.format('用戶名:',key))
print('{0:<10} {1}'.format('密碼:',users[key]))
for j in priv:
privs=j[0].replace('GRANT','')
privs_info=j[1]
print('{0} {1:<20} {2} {3}'.format('庫(表):',privs_info,'權限:',privs))
print('\n')
except Exception as err:
print(err)
continue
conn = mdb.connect(**config)
cursor = conn.cursor()
GetPriv()
cursor.close()
conn.close()
3、給執行權限
chmod +x
4、查看結果
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用戶名: ''@'localhost'
密碼:
庫(表): *.* 權限: USAGE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用戶名: 'root'@'192.168.1.%'
密碼: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
庫(表): *.* 權限: ALL PRIVILEGES
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用戶名: 'root'@'localhost'
密碼: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
庫(表): *.* 權限: ALL PRIVILEGES
庫(表): ''@'' 權限: PROXY