source: src/django_gheat/gheat/management/commands/netstumbler.py@ 9602

Last change on this file since 9602 was 9602, checked in by rick, 13 years ago

Saves a whole bunch of casting on a later stage.

  • Property svn:executable set to *
File size: 3.0 KB
RevLine 
[9600]1#!/usr/bin/env python
2#
3# Reading NS1 files - http://www.stumbler.net/ns1files.html
4#
5# Rick van der Zwet <info@rickvanderzwet.nl>
6#
7import sys
8import datetime
9
10from struct import *
11
[9601]12def parse_netstumbler(filename):
13 fh = open(filename,'rb')
14
15 def get_int32(size=1):
[9602]16 v = unpack('<' + 'i'*size,fh.read(4*size))
17 return v[0] if size == 1 else v
[9601]18
19 def get_uint32(size=1):
[9602]20 v = unpack('<' + 'I'*size,fh.read(4*size))
21 return v[0] if size == 1 else v
[9601]22
23 def get_uint64(size=1):
[9602]24 v = unpack('<' + 'Q'*size,fh.read(8*size))
25 return v[0] if size == 1 else v
[9601]26
27 def get_uint8(size=1):
[9602]28 v = unpack('<' + 'B'*size,fh.read(1*size))
29 return v[0] if size == 1 else v
[9601]30
31 def get_filetime():
32 _FILETIME_null_date = datetime.datetime(1601, 1, 1, 0, 0, 0)
33 ns = unpack('<Q',fh.read(8))[0] * 10
34 sec = ns / 10**8
35 d = datetime.timedelta(seconds=sec)
36 return _FILETIME_null_date + d
37
38 def get_char(size):
39 return fh.read(size)
40
41 def get_double(size=1):
[9602]42 v = unpack('<' + 'd'*size,fh.read(8*size))
43 return v[0] if size == 1 else v
[9601]44
45 data = {}
46
47 data["dwSignature"] = get_char(4)
48 data["dwFileVerunpack"] = get_uint32()
[9602]49 ApCount = get_uint32()
[9601]50 data["ApCount"] = ApCount
51
52 data["aps"] = []
53 for a in range(0,ApCount):
54 ap = {}
[9602]55 SSIDLength = get_uint8()
[9601]56 ap["SSIDLength"] = SSIDLength
57 ap["SSID"] = get_char(SSIDLength)
58 ap["BSSID"] = map(hex,unpack('BBBBBB',fh.read(6)))
59 ap["MaxSignal"] = get_int32()
60 ap["MinNoise"] = get_int32()
61 ap["MaxSNR"] = get_int32()
62 ap["Flags"] = get_uint32()
63 ap["BeaconInterval"] = get_uint32()
64 ap["FirstSeen"] = get_filetime()
65 ap["LastSeen"] = get_filetime()
66 ap["BestLat"] = get_double()
67 ap["BestLong"] = get_double()
[9602]68 DataCount = get_uint32()
[9601]69 ap["DataCount"] = DataCount
70 ap["measurement"] = []
71 for c in range(0,DataCount):
72 ms = {}
73 ms["Time"] = get_filetime()
74 ms["Signal"] = get_int32()
75 ms["Noice"] = get_int32()
[9602]76 LocationSource = get_int32()
[9601]77 ms["Location Source"] = LocationSource
78 if LocationSource == 1:
79 ms["Latitude"] = get_double()
80 ms["Longitude"] = get_double()
81 ms["Altitude"] = get_double()
82 ms["NumStats"] = get_uint32()
83 ms["Speed"] = get_double()
84 ms["Track"] = get_double()
85 ms["MagVariation"] = get_double()
86 ms["Hdop"] = get_double()
87 ap["measurement"].append(ms)
[9602]88 NameLength = get_uint8()
[9601]89 ap["NameLength"] = NameLength
90 ap["Name"] = get_char(NameLength)
91 ap["Channels"] = get_uint64()
92 ap["LastChannel"] = get_uint32()
93 ap["IPAddress"] = get_uint32()
94 ap["MinSignal"] = get_int32()
95 ap["MaxSignal"] = get_int32()
96 ap["DataRate"] = get_uint32()
97 ap["IPSubnet"] = get_uint32()
98 ap["IPMask"] = get_uint32()
99 ap["ApFlags"] = get_uint32()
[9602]100 IELength = get_uint32()
[9601]101 ap["IELength"] = IELength
102 ap["InformationElements"] = get_uint8(IELength)
103 data["aps"].append(ap)
104 return data
[9600]105
[9601]106print parse_netstumbler(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.