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
|
#!/usr/bin/python
# scan utmp and remove bogus entries
# works only for systems that have ut_pid
# linux has, BSD has not
import utmp
from UTMPCONST import *
import time, os, string
a = utmp.UtmpRecord()
print "Removing bogus entries:"
print "%-10s %-10s %5s %-25s %-20s" % ("USER", "TTY", "PID", "HOST", "LOGIN")
ps = os.popen("ps aux").readlines()[1:]
#ps = os.popen("ps -edf").readlines()[1:]
pids = {}
for i in ps:
user, pid = string.split(i)[:2]
pids[int(pid)] = user
while 1:
b = a.getutent()
if not b:
break
if b[0] == USER_PROCESS:
if (not pids.has_key(b[1])) or b[4]<>pids[b[1]]:
print "%-10s %-10s %5i %-25s %-20s" % (b[4], b[2], b[1], b[5], time.ctime(b[8][0]))
b = list(b)
b[0] = DEAD_PROCESS
b[4] = ''
b[8] = (0, 0)
a.pututline(b)
a.getutent() # to move to next entry
a.endutent()
|