createdb.py

Script for creating the SQLite with FTS3 database from a SIP trace file - Adrian Georgescu, 08/29/2010 01:33 pm

Download (1.6 kB)

 
1
#!/usr/bin/python2.6
2

    
3
import os
4
import re
5
import sqlite3
6
from datetime import datetime
7

    
8
try: os.remove('data.db')
9
except: pass
10
conn = sqlite3.connect('data.db')
11
cursor = conn.cursor()
12
cursor.execute('CREATE VIRTUAL TABLE sip_trace USING fts3(timestamp, direction, source_address, source_port, destination_address, destination_port, protocol, packet)')
13

    
14
second_line = False
15
second_line_re = re.compile(r'^(?P<date>.+?):\s+(?P<srcaddr>.+?):(?P<srcport>\d+?)\s*-\((?P<proto>.+?)\)->\s*(?P<dstaddr>.+?):(?P<dstport>\d+?)$')
16
data = None
17
packet = ''
18
direction = None
19
for line in open('../../../.sipclient/logs/sip_trace.txt.0'):
20
    line = line.strip()
21
    if second_line:
22
        second_line = False
23
        data = second_line_re.match(line).groupdict()
24
        data['date'] = datetime.strptime(data['date'], '%Y-%m-%d %H:%M:%S.%f')
25
        data['srcport'] = int(data['srcport'])
26
        data['dstport'] = int(data['dstport'])
27
    elif line.startswith('RECEIVED:'):
28
        direction = 'incoming'
29
        data = None
30
        packet = ''
31
        second_line = True
32
    elif line.startswith('SENDING:'):
33
        direction = 'outgoing'
34
        data = None
35
        packet = ''
36
        second_line = True
37
    elif line == '--':
38
        cursor.execute('INSERT INTO sip_trace (timestamp, direction, source_address, source_port, destination_address, destination_port, protocol, packet) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
39
                       (data['date'], direction, data['srcaddr'], data['srcport'], data['dstaddr'], data['dstport'], data['proto'], packet))
40
        direction = None
41
    else:
42
        packet += line + '\r\n'
43

    
44
conn.commit()
45
conn.close()