source: src/django_gheat/gheat/management/commands/import_kismet.py@ 9168

Last change on this file since 9168 was 9168, checked in by rick, 14 years ago

Make kismet import way more robust...

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1#!/usr/bin/env python
2#
3# Script for importing .gpsxml and .netxml files (Kismet output)
4#
5
6from django.core.management.base import BaseCommand,CommandError
7from optparse import OptionParser, make_option
8from gheat.models import *
9from lxml import etree
10import datetime
11import os
12import sys
13
14def import_file(gpsxml_file, netxml_file, meetrondje, gebruiker, email):
15 gpsxml_doc = etree.parse(gpsxml_file)
16 netxml_doc = etree.parse(netxml_file)
17
18 points = gpsxml_doc.findall('gps-point')
19 wnetworks = netxml_doc.findall('wireless-network')
20
21 # TODO: Source source is variable entitity, based on mesurement
22 kaart = 'deadcode'
23 gebruiker, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
24 apparatuur, created = Apparatuur.objects.get_or_create(antenne='test' , kaart=kaart)
25 # TODO: Date is set to import date, but should pick the date from the netxml file
26 mr = MeetRondje.objects.create(datum=datetime.datetime.now(),
27 naam=meetrondje , gebruiker=gebruiker , apparatuur=apparatuur)
28
29 # Create all accesspoints and for caching validation purposes store them
30 # locally as well
31 ap_cache = {}
32 ap_ignore = []
33 print "#INFO: Going to import %s accesspoints" % len(wnetworks)
34 for wnetwork in wnetworks:
35 bssid = wnetwork.find('BSSID').text
36 # Only store access points
37 if wnetwork.attrib['type'] != "infrastructure":
38 ap_ignore.append(bssid)
39 continue
40
41 enc = (wnetwork.find('SSID/encryption') != None)
42 ssid_node = wnetwork.find('SSID/essid[@cloaked="false"]')
43 ssid = ssid_node.text if ssid_node != None else 'hidden'
44
45 ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
46 ap_cache[bssid] = ap
47
48 count = 0
49 #XXX: This is not effient at all, try to wrap it into a a bulk insert would
50 # be much more effient as for example: http://djangosnippets.org/snippets/2362/
51 print "#INFO: Going to import %s points" % len(points)
52 for point in points:
53 #XXX: This needs to be either the 'bssid' or the 'source', accesspoint from or too data.
54 bssid = point.attrib['bssid']
55 # XXX: Filter this in the beginning with XPath, but etree does not support that (yet).
56 if bssid in ['GP:SD:TR:AC:KL:OG','00:00:00:00:00:00']:
57 continue
58 elif bssid in ap_ignore:
59 continue
60 elif not ap_cache.has_key(bssid):
61 try:
62 ap = Accespoint.objects.get(mac=bssid)
63 ap_cache[bssid] = ap
64 except Accespoint.DoesNotExist:
65 print "#ERROR: Cannot found SSID for BSSID '%s'" % bssid
66 continue
67
68 # XXX: Signal need properly be a relation of signal_dbm and noice_dbm
69 signaal = 100 + int(point.attrib['signal_dbm'])
70
71 meting= Meting.objects.create(meetrondje=mr, accespoint=ap_cache[bssid],
72 latitude=point.attrib['lat'], longitude=point.attrib['lon'],
73 signaal=signaal)
74 # Give some feedback to the user
75 count += 1
76 if (count % 1000) == 0:
77 sys.stdout.write(str(count))
78 elif (count % 100) == 0:
79 sys.stdout.write(".")
80 sys.stdout.flush()
81
82 sys.stdout.write("%s\n" % count)
83 print "#INFO: All done, goodbye"
84
85
86class Command(BaseCommand):
87 args = '<gpsxml> [<netxml>]'
88 option_list = BaseCommand.option_list + (
89 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje',help='Naam van het meetrondje'),
90 make_option('-g', '--gebruiker', dest='gebruiker', default='username',help='Naam van de persoon die de meting uitgevoerd heeft'),
91 make_option('-e', '--email', dest='email', default='foo@bar.org',help='Email van de persoon die de meting uitgevoerd heeft'),
92 )
93
94 def handle(self, *args, **options):
95 try:
96 if len(args) == 2:
97 (gpsxml_file, netxml_file) = args
98 elif len(args) == 1:
99 (gpsxml_file,) = args
100 netxml_file = gpsxml_file[:-6] + 'netxml'
101 else:
102 raise ValueError
103 except ValueError:
104 self.print_help(sys.argv[0],sys.argv[1])
105 raise CommandError("Not all arguments are provided")
106 if not os.path.isfile(gpsxml_file):
107 raise CommandError("gpsxml file '%s' does not exists" % gpsxml_file)
108 if not os.path.isfile(netxml_file):
109 raise CommandError("netxml file '%s' does not exists" % netxml_file)
110
111 import_file(gpsxml_file, netxml_file ,options['meetrondje'],options['gebruiker'],options['email'])
Note: See TracBrowser for help on using the repository browser.