Guest User

Untitled

a guest
Oct 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #!/usr/bin/python
  2. import pwd, grp, os, time
  3.  
  4. PRESERVEUSERS=["root", "daemon", "bin", "sys", "sync", "games", "man",
  5. "lp", "mail", "news", "uucp", "proxy", "www-data", "backup",
  6. "list", "irc", "gnats", "nobody", "libuuid", "ntp", "sshd"]
  7. DELETEAFTER = 86400 * 30
  8.  
  9. now = time.time()
  10.  
  11. for p in pwd.getpwall():
  12. username = p[0] #, grp.getgrgid(p[3])[0]
  13. if username in PRESERVEUSERS: continue
  14.  
  15. output_lines = os.popen("lastlog -u " + username).read().split("\n")
  16. if len(output_lines) < 2: continue # no login data
  17. output = output_lines[1]
  18. if "Never logged in" in output: continue # never logged in
  19.  
  20. words = output.split()
  21. #if not words: continue # no login data
  22.  
  23. #print words
  24.  
  25. lastlog = " ".join([words[4], words[5], words[8]])
  26. lastlog_t = time.mktime(time.strptime(lastlog, "%b %d %Y"))
  27. if now - lastlog_t > DELETEAFTER:
  28. #os.system("usermod -L " + username)
  29. print "Locking user=%s lastlog=%s" % (username, lastlog)
  30. else:
  31. print "Skip user=%s lastlog=%s" % (username, lastlog)
Add Comment
Please, Sign In to add comment