2
#---------------------------------------------------------
#  Print days diff by Converting Unix timestamp to Readable Date/time
#---------------------------------------------------------
def convUnixTime(t):
        return 1+(datetime.datetime.fromtimestamp(t*60*60*24)
              - datetime.datetime.today()).days


#---------------------------------------------------------
# Read shadow file and check for account expires and create dictionary 
#---------------------------------------------------------
with open( "/etc/shadow" ) as shadow:
        for aLine in shadow:
                filed = aLine.split(":")
                f = filed[7]
                try:
                    f = int(f)
                    f=convUnixTime(f)
                except ValueError:
                    f = "NULL"
                if f != "NULL" and f <= 0:
                        total_expired_users += 1
                        expr_list[ filed[0] ] = f
                elif f != "NULL" and f <= min_days:
                        total_expring_users += 1
                        expr_list[ filed[0] ] = f

我已经创建了帐户已过期的用户字典,但我认为这已经以更清洁和简单的方式完成了..

提前致谢!!

4

3 回答 3

1

使用try-except子句可能看起来更简洁:

try:
    f = int(f)
    f=convUnixTime(f)
except ValueError:
    pass
else:
    if f <= 0:
      total_expired_users += 1
      expr_list[ filed[0] ] = f
    elif f <= min_days:
      total_expring_users += 1
      expr_list[ filed[0] ] = f

您还可以稍微更改顺序,以避免expr_list[filed[0]]重复:

if f <= min_days:
    expr_list[filed[0]] = f

    if f <= 0:
        total_expired_users += 1
    else:
        total_expiring_users += 1
于 2013-06-13T04:39:00.413 回答
1

您可以使用列表推导来简化您的代码:

import datetime

min_days = 20000

def conv_unix_time(t):
    return (1+(datetime.datetime.fromtimestamp(int(t)*60*60*24)
            - datetime.datetime.today()).days) if t else None

with open('foo.txt') as shadow:
    expire_durations = {line.split(':')[0]: conv_unix_time(line.split(':')[7]) for line in shadow}

print [user for user, time in expire_durations.iteritems() if time and time < 0]
print [user for user, time in expire_durations.iteritems() if time and 0 < time < min_days]
于 2013-06-13T05:42:11.130 回答
0

谢谢贝雷亚尔,

在您的更正的帮助下,我创建了最终..

#---------------------------------------------------------
#  Convert Unix timestamp to Readable Date/time
#---------------------------------------------------------
def convUnixTime(t):
        return datetime.datetime.fromtimestamp(t*60*60*24)


#---------------------------------------------------------
# Read shadow file and check for account expriry
#---------------------------------------------------------
with open( "/etc/shadow" ) as shadow:
        for aLine in shadow:
                filed = aLine.split(":")
                f = filed[7]
                try:
                        f = int(f)
                        exprdate = convUnixTime(f)
                        f=1+( exprdate - datetime.datetime.today()).days
                        l=[f,exprdate]
                except ValueError:
                        pass
                else:
                        if f <= min_days:
                                expr_list[filed[0]]=l
                        if f <= 0:
                                total_expired_users += 1
                        else:
                                total_expring_users += 1

解析整个脚本后..输出:

====>>Following 3 Account has been Expired
rahul    :: Expired on 2013-06-12 05:30:00
test     :: Expired on 2013-06-01 05:30:00
vinayak  :: Expired on 1970-01-01 05:30:00
====>>Following 4 Account will be Expire in 20 Days
suraj    :: 2  days || Last date :: 2013-06-15 05:30:00
ram      :: 1  days || Last date :: 2013-06-14 05:30:00
javed    :: 2  days || Last date :: 2013-06-15 05:30:00
shyam    :: 2  days || Last date :: 2013-06-15 05:30:00
于 2013-06-13T05:42:02.280 回答