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
网站题目:mysql查看用户的权限脚本
文章路径:
http://bzwzjz.com/article/jspsop.html