我刚刚解决了一个问题,如果用户的密码在14天内到期,就会通知他们。剧本运行得很好。我只想改进我的代码,并希望得到一些建议。
#!/usr/bin/env python
# Author :- Rahul Patil<linuxian.com>
#
import sys
import os
#-----------------------------------------------------------------
# Provide Ldap DN Details to Perform Ldap Search using anonymouse
#-----------------------------------------------------------------
Domain = 'linuxian.com'
EmailDomain = 'abc.om' # if your mail domain is different this will becom user@abc.com
ConnectDC = 'ldap://localhost:389'
# if 14 days remains to expire password then it will send email to that user
# until user update the new password
PwdWarnDays = 14
pwdMaxAge = 45 # default password expire in 45 days as per ldap ppolicy
Subject = "Ldap Password Expiry Details"
MsgBody = """
Dear %s,
Your Password Will be Expire in %s, we request you to please
change your password, your last password change date is %s
Best Regards,
Linux Admin
"""
def GetUserDetails():
""" This Function Will save all details in file
it will use ldap search query for the same."""
# Create bind dn eg. dc=example,dc=com
BindDN = ','.join([ 'dc=' + d for d in Domain.split('.') ])
#
import ldap
l = ldap.initialize(ConnectDC)
# Perform Ldap Search
return l.search_s(
BindDN,
ldap.SCOPE_SUBTREE,
'(uid=*)',['uid','pwdChangedTime']
)
def CheckExpiry():
"""
This Function will Check each user ExpiryWarning
if it more thans WarningDays then it will send Emails
to that particuler user
"""
import datetime
for k,v in Users:
uid = ''.join(v['uid'])
if 'pwdChangedTime' not in v:
pass
#print "User " + uid + " not Updated Password"
try:
l = ''.join(v['pwdChangedTime'])
except:
pass
if 'pwdChangedTime' in v:
# To extrace year month day
d1 = datetime.date.today()
d2 = datetime.date(int(l[0:4]),int(l[4:6]),int(l[6:8]))
DaysOfPasswordChange = (d1 - d2).days
d2 = d2.strftime('%d, %b %Y')
ExpireIn = pwdMaxAge - DaysOfPasswordChange
# if password not changed before 14 days
if ExpireIn <= PwdWarnDays:
SendMail = "echo '" + MsgBody % (uid,ExpireIn,d2) + "' \
mail -s " + '"' + Subject + '"' + ' ' + \
uid + '@' + EmailDomain
#os.system(SendMail)
print SendMail
if __name__ == '__main__':
Users = GetUserDetails()
CheckExpiry()发布于 2013-10-09 14:46:26
你应该通读Python样式指南。您的代码不遵循大多数Python代码使用的命名约定。
单字母变量应该谨慎使用,因为它们可能不像您想象的那样直观。更有描述性的名字更好。
导入通常应该在文件的顶部进行。
string.format()可能比您在CheckExpiry()末尾所做的字符串连接更易读。
您的文档字符串以“此函数将”开头。这是不必要的,因为文档总是描述函数的功能。
在脚本的开头,定义设置日期边界的常量。这很好。但是,您可以在注释中重复使用该值。这很糟糕,因为如果策略更改为在21天发出警告,则除了常量的值外,还需要更新注释。
你不需要对每一个操作都发表评论。一般来说,评论更能说明你为什么要做某件事,而不是说你在做什么。
# if password not changed before 14 days
if ExpireIn <= PwdWarnDays:您有很好的变量名,因此这一行代码解释了自己,注释没有添加任何内容。如果您有一行代码非常复杂,您觉得需要记录它正在做的事情,这表明可能有更好的方法来完成它。
发布于 2013-11-19 06:57:13
我会添加"bind_s“来与用户连接到ldap。
user_dn = 'someuser'
user_pwd = 'somepassword'
l = ldap.initialize(ConnectDC)
l.bind_s(user_dn, user_pwd) #LDAP admin would not allow to query pwdChangedTime as anonymous并修改一些代码,使其在发送邮件时更加美观:
return l.search_s(bind_dn, ldap.SCOPE_SUBTREE, '(uid=*)', ['displayName', 'pwdChangedTime', 'uid'])
for k, v in Users:
uid = ''.join(v['uid'])
if 'displayName' in v:
displayname = ''.join(v['displayName'])
else:
displayname = uid
...
if 'pwdChangedTime' in v:
...
else:
d2 = '[You have not changed your password since account created]'
...
print mail_body % (displayname, pwd_expire_in_days, d2)发布于 2015-03-20 05:24:10
我将使用if __name__ == '__main__':将函数定义从脚本功能中分离出来,这是Python的惯例。此外,我希望CheckExpiry()将“用户”作为参数,并返回过期用户列表(或每个用户的名称和电子邮件地址的元组)。这样看上去更像是:
#!python
# ....
if __name__ == '__main__':
userdata = GetUserDetails()
expired = CheckExpiry(userdata)
# ...在那里,我创建了一个模板(可能像使用Python标准库中的string.Template一样简单,或者可能使用类似于Jinja2的东西)。并使用它生成给每个用户的消息。
如果您真的想得到喜欢,您可以跟踪您发送给每个用户的警告数量,可能在一个非常简单的本地平面文件数据库中使用SQLite3 (也来自标准库)。
https://codereview.stackexchange.com/questions/32473
复制相似问题