last improvements + last now uses a plain text file instead of anydbm

git-svn-id: https://kippo.googlecode.com/svn/trunk@150 951d7100-d841-11de-b865-b3884708a8e2
This commit is contained in:
desaster
2010-06-29 20:00:02 +00:00
parent d8b1b0df63
commit 0d70a7b4e0
2 changed files with 34 additions and 12 deletions

View File

@@ -11,14 +11,21 @@ commands = {}
class command_last(HoneyPotCommand): class command_last(HoneyPotCommand):
def call(self): def call(self):
db = anydbm.open('%s/lastlog.db' % \ fn = '%s/lastlog.txt' % (config().get('honeypot', 'data_path'),)
config().get('honeypot', 'data_path'), 'c') if not os.path.exists(fn):
count = 0 return
for k in sorted(db.keys(), key=int, reverse=True): l = list(self.args)
self.writeln(db[k]) numlines = 25
count += 1 while len(l):
if count >= 25: arg = l.pop(0)
break if not arg.startswith('-'):
continue
elif arg[1:].isdigit():
numlines = int(arg[1:])
elif arg == '-n' and len(l) and l[0].isdigit():
numlines = int(l.pop(0))
data = utils.tail(file(fn), numlines)
self.writeln(''.join(data))
commands['/usr/bin/last'] = command_last commands['/usr/bin/last'] = command_last
# vim: set sw=4 et: # vim: set sw=4 et:

View File

@@ -5,10 +5,9 @@ import time, anydbm
from kippo.core.config import config from kippo.core.config import config
def addToLastlog(message): def addToLastlog(message):
db = anydbm.open('%s/lastlog.db' % \ f = file('%s/lastlog.txt' % config().get('honeypot', 'data_path'), 'a')
config().get('honeypot', 'data_path'), 'c') f.write('%s\n' % (message,))
db[str(len(db)+1)] = message f.close()
db.close()
def durationHuman(seconds): def durationHuman(seconds):
seconds = long(round(seconds)) seconds = long(round(seconds))
@@ -38,4 +37,20 @@ def durationHuman(seconds):
return ''.join(duration) return ''.join(duration)
# From http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail
def tail(the_file, lines_2find=20):
the_file.seek(0, 2) #go to end of file
bytes_in_file = the_file.tell()
lines_found, total_bytes_scanned = 0, 0
while lines_2find+1 > lines_found and bytes_in_file > total_bytes_scanned:
byte_block = min(1024, bytes_in_file-total_bytes_scanned)
the_file.seek(-(byte_block+total_bytes_scanned), 2)
total_bytes_scanned += byte_block
lines_found += the_file.read(1024).count('\n')
the_file.seek(-total_bytes_scanned, 2)
line_list = list(the_file.readlines())
return line_list[-lines_2find:]
#we read at least 21 line breaks from the bottom, block by block for speed
#21 to ensure we don't get a half line
# vim: set sw=4 et: # vim: set sw=4 et: