-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpire.py
79 lines (72 loc) · 3.27 KB
/
expire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def show_password_expire(show_expired=True, session=None):
import mysqlsh
shell = mysqlsh.globals.shell
if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return
stmt = "select @@default_password_lifetime, @@disconnect_on_expired_password"
result = session.run_sql(stmt)
rows = result.fetch_all()
if rows[0][0] == 0:
print "Default password doesn't expire"
else:
print "Default password expires in %d days" % rows[0][0]
if rows[0][1] == 1:
print "On expired password disconnect"
if show_expired:
stmt = """select concat(sys.quote_identifier(user),'@',sys.quote_identifier(host))
AS user,
password_last_changed, IF((cast(
IFNULL(password_lifetime, @@default_password_lifetime) as signed)
+ cast(datediff(password_last_changed, now()) as signed) > 0),
concat(
cast(
IFNULL(password_lifetime, @@default_password_lifetime) as signed)
+ cast(datediff(password_last_changed, now()) as signed), ' days'),
IF(@@default_password_lifetime > 0, 'expired', 'do not expire')) expires_in
from mysql.user
where
user not like 'mysql.%' order by user"""
else:
stmt = """select concat(sys.quote_identifier(user),'@',sys.quote_identifier(host))
AS user,
password_last_changed,
concat(
cast(
IFNULL(password_lifetime, @@default_password_lifetime) as signed)
+ cast(datediff(password_last_changed, now()) as signed), ' days') expires_in
from mysql.user
where
cast(
IFNULL(password_lifetime, @@default_password_lifetime) as signed)
+ cast(datediff(password_last_changed, now()) as signed) > 0
and user not like 'mysql.%' order by user;"""
result = session.run_sql(stmt)
shell.dump_rows(result)
def show_password_expire_soon(expire_in_days=30, session=None):
import mysqlsh
shell = mysqlsh.globals.shell
if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return
stmt = """select concat(sys.quote_identifier(user),'@',sys.quote_identifier(host))
AS user,
password_last_changed,
concat(
cast(
IFNULL(password_lifetime, @@default_password_lifetime) as signed)
+ cast(datediff(password_last_changed, now()) as signed), ' days') expires_in
from mysql.user
where
cast(
IFNULL(password_lifetime, @@default_password_lifetime) as signed)
+ cast(datediff(password_last_changed, now()) as signed) BETWEEN 1 and {limit}
and user not like 'mysql.%' order by user;""".format(limit=expire_in_days)
result = session.run_sql(stmt)
shell.dump_rows(result)