File: scanutmp.py

package info (click to toggle)
python-utmp 0.7.4-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 176 kB
  • ctags: 189
  • sloc: python: 773; ansic: 568; makefile: 43; sh: 2
file content (32 lines) | stat: -rwxr-xr-x 891 bytes parent folder | download | duplicates (2)
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
#!/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


for b in a:
    if b.ut_type == USER_PROCESS:
        if (not pids.has_key(b.ut_pid)) or b.ut_user<>pids[b.ut_pid]:
            print "%-10s %-10s %5i %-25s %-20s" % (b.ut_user, b.ut_line, b.ut_pid, b.ut_host, time.ctime(b.ut_tv[0]))
            b.ut_type = DEAD_PROCESS
            b.ut_host = ''
            b.ut_tv = (0, 0)
            a.pututline(b)
            a.getutent() # to move to next entry