author | Tero Marttila <terom@paivola.fi> |
Fri, 21 Mar 2008 02:06:34 +0200 | |
changeset 9 | 75cc996c6ba1 |
parent 8 | 3d648daf9538 |
child 11 | 2d33d62cd8f8 |
permissions | -rw-r--r-- |
8 | 1 |
import socket, os, time |
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
import _utmp |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
4 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
5 |
class WtmpEntry (object) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
6 |
_ATTRS = ( |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
7 |
"type", "pid", "line", "id", "user", "host", "exit", "session", "tv", "addr_v6" |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
8 |
) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
9 |
|
8 | 10 |
@staticmethod |
11 |
def fromFile (file) : |
|
12 |
bytes = file.read(_utmp.size()) |
|
13 |
||
14 |
if not bytes : |
|
15 |
return |
|
16 |
||
17 |
result = _utmp.parse(bytes) |
|
18 |
||
19 |
wtmp = WtmpEntry() |
|
20 |
||
21 |
for name, field in zip(wtmp._ATTRS, result) : |
|
22 |
if isinstance(field, str) and name not in ("addr_v6", ) : |
|
23 |
field = field.rstrip("\0") |
|
24 |
||
25 |
setattr(wtmp, name, field) |
|
26 |
||
27 |
wtmp.addr_v6 = socket.inet_ntop(socket.AF_INET6, wtmp.addr_v6) |
|
28 |
||
29 |
return wtmp |
|
30 |
||
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
31 |
def __str__ (self) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
32 |
return " ".join("%s=%s" % (key, getattr(self, key)) for key in self._ATTRS) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
33 |
|
8 | 34 |
class WtmpFile (object) : |
35 |
def __init__ (self, path="/var/log/wtmp") : |
|
36 |
self.path = path |
|
37 |
self.file = open(path) |
|
38 |
self.file.seek(0, os.SEEK_END) |
|
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
39 |
|
8 | 40 |
def tryRead (self) : |
41 |
return WtmpEntry.fromFile(self.file) |
|
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
42 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
43 |
def read_entries (file) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
44 |
while True : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
45 |
wtmp = read_entry(file) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
46 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
47 |
if wtmp : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
48 |
yield wtmp |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
49 |
else : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
50 |
return |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
51 |
|
8 | 52 |
def follow_main () : |
53 |
wtmp = WtmpFile() |
|
54 |
||
55 |
while True : |
|
56 |
item = wtmp.tryRead() |
|
57 |
||
58 |
if item : |
|
59 |
print item |
|
60 |
else : |
|
61 |
time.sleep(2) |
|
62 |
||
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
63 |
def test_main () : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
64 |
fh = open("/var/log/wtmp", "r") |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
65 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
66 |
for item in read_entries(fh) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
67 |
print item |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
68 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
69 |
if __name__ == '__main__' : |
8 | 70 |
follow_main() |
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
71 |