Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import pwd, grp, os, time
- PRESERVEUSERS=["root", "daemon", "bin", "sys", "sync", "games", "man",
- "lp", "mail", "news", "uucp", "proxy", "www-data", "backup",
- "list", "irc", "gnats", "nobody", "libuuid", "ntp", "sshd"]
- DELETEAFTER = 86400 * 30
- now = time.time()
- for p in pwd.getpwall():
- username = p[0] #, grp.getgrgid(p[3])[0]
- if username in PRESERVEUSERS: continue
- output_lines = os.popen("lastlog -u " + username).read().split("\n")
- if len(output_lines) < 2: continue # no login data
- output = output_lines[1]
- if "Never logged in" in output: continue # never logged in
- words = output.split()
- #if not words: continue # no login data
- #print words
- lastlog = " ".join([words[4], words[5], words[8]])
- lastlog_t = time.mktime(time.strptime(lastlog, "%b %d %Y"))
- if now - lastlog_t > DELETEAFTER:
- #os.system("usermod -L " + username)
- print "Locking user=%s lastlog=%s" % (username, lastlog)
- else:
- print "Skip user=%s lastlog=%s" % (username, lastlog)
Add Comment
Please, Sign In to add comment