1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | ###########################################
|
---|
4 | #
|
---|
5 | # Script for importing .netxml files (Kismet output)
|
---|
6 | #
|
---|
7 | # In theory, only the -f option is needed, but for overview's sake, please use the others aswell.
|
---|
8 | # -f = location of the .netxml, e.g. '/home/test.netxml'
|
---|
9 | # -m = name of the dataset, e.g. 'Walk in park' or 'Trip with boat'
|
---|
10 | # -g = your name
|
---|
11 | # -e = your email address
|
---|
12 | #
|
---|
13 | # (Run from project root)
|
---|
14 | # ./manage.py dataimport -f <file location> -m <dataset name> -g <username> -e <email>
|
---|
15 | #
|
---|
16 | # Make sure your file has atleast the following elements:
|
---|
17 | #
|
---|
18 | # <wireless-network>
|
---|
19 | # <SSID>
|
---|
20 | # <encryption> </encryption>
|
---|
21 | # <essid cloaked=""> </essid>
|
---|
22 | # </SSID>
|
---|
23 | # <BSSID> </BSSID>
|
---|
24 | # <gps-info>
|
---|
25 | # <min-lat> </min-lat>
|
---|
26 | # <min-lon> </min-lon>
|
---|
27 | # </gps-info>
|
---|
28 | # </wireless-network>
|
---|
29 | #
|
---|
30 | #
|
---|
31 | # Dennis Wagenaar
|
---|
32 | # d.wagenaar@gmail.com
|
---|
33 | #
|
---|
34 | ###########################################
|
---|
35 |
|
---|
36 | from django.core.management import setup_environ
|
---|
37 | from django.core.management.base import BaseCommand
|
---|
38 | from optparse import OptionParser, make_option
|
---|
39 | import settings
|
---|
40 | setup_environ(settings)
|
---|
41 | from gheat.models import *
|
---|
42 | from lxml import etree
|
---|
43 | import datetime
|
---|
44 |
|
---|
45 | def import_file(location, meetrondje, gebruiker, email):
|
---|
46 |
|
---|
47 | datasource = etree.parse(location)
|
---|
48 | doc = datasource.findall('wireless-network')
|
---|
49 | kaart = datasource.find('card-source/card-source')
|
---|
50 | kaart = kaart.text
|
---|
51 | g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
|
---|
52 | a, created = Apparatuur.objects.get_or_create(antenne='test' , kaart=kaart)
|
---|
53 | mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
|
---|
54 |
|
---|
55 | for wnetwork in doc:
|
---|
56 | enc = wnetwork.find('SSID/encryption')
|
---|
57 | if enc != None: enc = 'true'
|
---|
58 | else: enc = 'false'
|
---|
59 | ssid = wnetwork.find('SSID/essid')
|
---|
60 | if ssid != None: ssid = ssid.text
|
---|
61 | else: ssid = 'hidden'
|
---|
62 | bssid = wnetwork.find('BSSID')
|
---|
63 | if bssid != None: bssid = bssid.text
|
---|
64 | lat = wnetwork.find('gps-info/min-lat')
|
---|
65 | if lat != None: lat = lat.text
|
---|
66 | lon = wnetwork.find('gps-info/min-lon')
|
---|
67 | if lon != None: lon = lon.text
|
---|
68 | if ssid is None: continue
|
---|
69 | print enc, ssid, bssid, lat, lon
|
---|
70 |
|
---|
71 | ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
|
---|
72 | m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=100)
|
---|
73 |
|
---|
74 |
|
---|
75 | class Command(BaseCommand):
|
---|
76 | option_list = BaseCommand.option_list + (
|
---|
77 | make_option('-f', '--location', dest='location', default='location'),
|
---|
78 | make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
|
---|
79 | make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
|
---|
80 | make_option('-e', '--email', dest='email', default='foo@bar.org'),
|
---|
81 | )
|
---|
82 |
|
---|
83 | def handle(self, *args, **options):
|
---|
84 | import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'])
|
---|