Ignore:
Timestamp:
May 7, 2011, 8:06:03 PM (14 years ago)
Author:
rick
Message:

New netstumber import, based on gpsxml and netxml input files.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • src/django_gheat/gheat/management/commands/import_kismet.py

    r9143 r9158  
    11#!/usr/bin/env python
    22#
    3 ###########################################
     3# Script for importing .gpsxml and .netxml files (Kismet output)
    44#
    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 ###########################################
    355
    36 from django.core.management import setup_environ
    37 from django.core.management.base import BaseCommand
     6from django.core.management.base import BaseCommand,CommandError
    387from optparse import OptionParser, make_option
    39 import settings
    40 setup_environ(settings)
    418from gheat.models import *
    429from lxml import etree
    4310import datetime
     11import os
     12import sys
    4413
    45 def import_file(location, meetrondje, gebruiker, email):
     14def import_file(gpsxml_file, netxml_file, meetrondje, gebruiker, email):
     15  gpsxml_doc = etree.parse(gpsxml_file)
     16  netxml_doc = etree.parse(netxml_file)
    4617
    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)
     18  points = gpsxml_doc.findall('gps-point')
     19  wnetworks = netxml_doc.findall('wireless-network')
    5420
    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     min_sig = wnetwork.find('snr-info/min_signal_dbm')
    65     if min_sig != None: min_sig = int(min_sig.text)
    66     else: min_sig = (-99)
    67     max_sig = wnetwork.find('snr-info/max_signal_dbm')
    68     if max_sig != None: max_sig = int(max_sig.text)
    69     else: max_sig= (-99)
    70     lat = wnetwork.find('gps-info/min-lat')
    71     if lat != None: lat = lat.text
    72     lon = wnetwork.find('gps-info/min-lon')
    73     if lon != None: lon = lon.text
    74     if ssid is None: continue
    75    
    76     avg_sig = (min_sig+max_sig)/2
    77     sig = (1.67*avg_sig)+166.67
    78     if sig > 100: sig = 100
    79     if sig < 0: sig = 0
     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)
    8028
    81     print enc, ssid, bssid, lat, lon, sig
     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'
    8244
    8345    ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
    84     m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=sig)
     46    ap_cache[bssid] = ap
    8547
     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: Filter this in the beginning with XPath, but etree does not support that (yet).
     54    if point.attrib['bssid'] in ['GP:SD:TR:AC:KL:OG','00:00:00:00:00:00']:
     55      continue
     56    if point.attrib['bssid'] in ap_ignore:
     57      continue
     58    ap = ap_cache[point.attrib['bssid']]
     59
     60    # XXX: Signal need properly be a relation of signal_dbm and noice_dbm
     61    signaal = 100 + int(point.attrib['signal_dbm'])
     62
     63    meting,created = Meting.objects.get_or_create(meetrondje=mr, accespoint=ap,
     64      latitude=point.attrib['lat'], longitude=point.attrib['lon'],
     65      signaal=signaal)
     66    # Give some feedback to the user
     67    if created:
     68      count += 1
     69      if (count % 1000) == 0:
     70        print count,
     71      elif (count % 100) == 0:
     72        print ".",
    8673
    8774class Command(BaseCommand):
     75  args = '<gpsxml> <netxml>'
    8876  option_list = BaseCommand.option_list + (
    89     make_option('-f', '--location', dest='location', default='location'),
    90     make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
    91     make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
    92     make_option('-e', '--email', dest='email', default='foo@bar.org'),
     77    make_option('-m', '--meetrondje', dest='meetrondje', default='rondje',help='Naam van het meetrondje'),
     78    make_option('-g', '--gebruiker', dest='gebruiker', default='username',help='Naam van de persoon die de meting uitgevoerd heeft'),
     79    make_option('-e', '--email', dest='email', default='foo@bar.org',help='Email van de persoon die de meting uitgevoerd heeft'),
    9380    )
    9481
     82  # TODO: Condider netxml file to be the gpsxml file with diffent suffix.
    9583  def handle(self, *args, **options):
    96     import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'])
     84    try:
     85      (gpsxml_file, netxml_file) = args
     86    except ValueError:
     87      self.print_help(sys.argv[0],sys.argv[1])
     88      raise CommandError("Not all arguments are provided")
     89    if not os.path.isfile(gpsxml_file):
     90      raise CommandError("gpsxml file '%s' does not exists" % gpsxml_file)
     91    if not os.path.isfile(netxml_file):
     92      raise CommandError("netxml file '%s' does not exists" % netxml_file)
     93
     94    import_file(gpsxml_file, netxml_file ,options['meetrondje'],options['gebruiker'],options['email'])
Note: See TracChangeset for help on using the changeset viewer.