author | Tero Marttila <terom@paivola.fi> |
Fri, 21 Mar 2008 01:53:51 +0200 | |
changeset 7 | 6a49fc285842 |
child 8 | 3d648daf9538 |
permissions | -rw-r--r-- |
7
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
import socket |
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 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
10 |
def __str__ (self) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
11 |
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
|
12 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
13 |
def read_entry (file) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
14 |
bytes = file.read(_utmp.size()) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
15 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
16 |
if not bytes : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
17 |
return |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
18 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
19 |
result = _utmp.parse(bytes) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
20 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
21 |
wtmp = WtmpEntry() |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
22 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
23 |
for name, field in zip(wtmp._ATTRS, result) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
24 |
if isinstance(field, str) and name not in ("addr_v6", ) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
25 |
field = field.rstrip("\0") |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
26 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
27 |
setattr(wtmp, name, field) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
28 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
29 |
wtmp.addr_v6 = socket.inet_ntop(socket.AF_INET6, wtmp.addr_v6) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
30 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
31 |
return wtmp |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
32 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
33 |
def read_entries (file) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
34 |
while True : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
35 |
wtmp = read_entry(file) |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
36 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
37 |
if wtmp : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
38 |
yield wtmp |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
39 |
else : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
40 |
return |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
41 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
42 |
def test_main () : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
43 |
fh = open("/var/log/wtmp", "r") |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
44 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
45 |
for item in read_entries(fh) : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
46 |
print item |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
47 |
|
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
48 |
if __name__ == '__main__' : |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
49 |
test_main() |
6a49fc285842
C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
50 |