Just another Command Line junkie blogNotes of a command line junkie
You should be using Firefox. Sat, 25 Oct 2008
Pyparsing atftpd log
Есть лог atftpd (самый себе обыкновенный, пишется через syslog).
1 #/usr/bin/python2.5
2 import pyparsing as pp
3 import calendar
4
5 atftpd_log_path = './atftpd.log'
6
7 tftp_files = ('file1', 'file2', 'file3')
8
9 def parse_atftpd_log(cfgpath):
10 month_abbr = pp.oneOf(list(calendar.month_abbr))
11 time = pp.Combine(pp.Word(pp.nums,exact=2) + (":" + pp.Word(pp.nums,exact=2))*2)
12 date = month_abbr + pp.Word(pp.nums,exact=2) + time
13
14 server_host = pp.Word(pp.alphanums + '-')
15 process = pp.Word(pp.printables)
16
17 file = pp.oneOf(tftp_files)("file")
18 ip = pp.Combine((pp.Word(pp.nums) +pp.Literal('.')) * 3 + pp.Word(pp.nums))("ip_address")
19
20 atftpd_entry = date + server_host + process + pp.Literal('Serving') +\
21 file + pp.Literal('to') + ip + pp.Combine(':' + pp.Word(pp.nums))
22
23 f = open(cfgpath, 'rb')
24 ppresults = atftpd_entry.scanString(''.join(f.readlines()))
25 f.close()
26
27 res = {}
28 for result in ppresults:
29 if res.has_key(result[0].ip_address) and result[0].file not in res[result[0].ip_address]:
30 res[result[0].ip_address].append(result[0].file)
31 else:
32 res[result[0].ip_address] = [result[0].file]
33 return res
34
35 tftp_hosts = parse_atftpd_log(atftpd_log_path)
36
37 for h in tftp_hosts.iterkeys():
38 print h + '\t' + ', '.join(tftp_hosts[h])
|
Menu Links RSS 2.0 ![]() |