-
Notifications
You must be signed in to change notification settings - Fork 0
/
aprs.py
239 lines (213 loc) · 6.96 KB
/
aprs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from __future__ import division
"""
parse some APRS-IS data to debug aprsd.c
you can get a live stream from telnet:noam.aprs2.net:14580
"""
__author__="Alan Crosswell <[email protected]>"
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2013 Alan Crosswell
"""
import re
import sys
from collections import defaultdict
from telnetlib import Telnet
import time
import pickle
from pprint import pprint
import bz2
class Packet:
"""
An AX.25 packet as received from an APRS-IS server.
"""
def __init__(self,from_call='',gate='',info='',full='',clock=0,gtype='',digis='',to_call=''):
self.from_call = from_call
self.to_call = to_call
self.digis = digis
self.gate = gate
self.gtype = gtype
self.info = info
self.full = full
self.clock = clock
def __repr__(self):
s = 'Packet(from_call="{}",gate="{}",info="{}",full="""{}""",clock={})'
return s.format(self.from_call,self.gate,self.info.encode('string_escape'),
self.full.encode('string_escape'),self.clock)
def __eq__(self,other):
"""compare only from_call and info fields for equality"""
return self.from_call == other.from_call and self.info == other.info
def __ne__(self,other):
"""compare only from_call and info fields for equality"""
return self.from_call != other.from_call or self.info != other.info
def __str__(self):
"""pretty string representation of the packet"""
return """Packet from: {}
to: {}
digis: {}
gtype: {}
gate: {}
({:4d}) info: "{}"
full: "{}"
clock: {}
""".format(self.from_call,self.to_call,self.digis,self.gtype,self.gate,len(self.info),
self.info.encode('string_escape'),self.full.encode('string_escape'),time.ctime(self.clock))
def pprint(self):
"""prety print"""
print self
def parse(l,clock=None):
"""parse a packet into constituent parts and insert into call[<callsign>]
returns from_call"""
s = re.match('^(?P<from_call>[^>]+)>(?P<to_call>[^,]+),*(?P<digis>.*),(?P<gtype>[^,]+),(?P<gate>[^:]+)(:)(?P<info>.*)$',l)
if s:
fc = s.group('from_call')
if fc not in call: print fc
tm = int(time.time()) if clock == None else clock;
call[fc].append(Packet(from_call=fc,gate=s.group('gate'),info=s.group('info'),
full=l,clock=tm,gtype=s.group('gtype'),
digis=s.group('digis').split(','),to_call=s.group('to_call')))
return fc
else:
if l[0] != '#': print 'Eh? >>>{}<<<'.format(l)
return None
def check(fc,quiet=False,diff=None):
"""
check fromcall <fc> to see if there are other packets that mostly match.
diff defines how much of a difference in length to consider for statistics-gathering
or None to count all mismatches.
mostly match is defined as having extra whitespace on the end of the info.
returns (mismatches,[list of 'bad' gates])
"""
mismatches = 0
badgates = set()
if fc not in call: return 0
calls = call[fc]
for i in range(len(calls)):
for j in range(i,len(calls)):
if i != j and calls[i] != calls[j]:
a = calls[i].info
b = calls[j].info
lendiff = abs(len(a)-len(b))
if a.rstrip(' \r') == b.rstrip(' \r'):
if (not diff) or (diff and lendiff == diff):
mismatches += 1
if (len(a)>len(b)):
badgates.add(calls[i].gate)
else:
badgates.add(calls[j].gate)
if not quiet:
print ''.ljust(40,'>')
print '{}:{}'.format(i,calls[i])
print '{}:{}'.format(j,calls[j])
print ''.ljust(40,'<')
return (mismatches,badgates)
def checkall(quiet=True,diff=None):
"""
iterate over all callsigns and check for incorrectly-padded packets.
returns (percentage,[set of bad gates])
"""
mismatches = 0
packets = 0
badgates = set()
for c in call:
packets += len(call[c])
(m,b) = check(c,quiet=quiet,diff=diff)
mismatches += m
badgates.update(b)
print '{} mismatches of len {} out of {} packets ({:.2f}%)'.format(mismatches,diff,packets,(mismatches*100)/packets)
print '{} bad gateways'.format(len(badgates))
return (mismatches/packets,badgates)
def guess_gate_type(badgates):
"""
Try to guess what type of gate is in the badgates set
"""
for bg in badgates:
pass
def invert():
for c,p in call.iteritems():
for packet in p:
gate[packet.gate].append(packet)
def logout(tn):
"""logout of telnet session. Sends a ^D and flushes the buffer"""
tn.write('\0x04')
counter = 0
out = ''
for l in tn.read_some():
out += l
counter += 1
if counter > 1000:
break
print out
tn.close()
def dump(call,fn):
pk=open(fn,'wb')
pickle.dump(call,pk)
pk.close()
def load(fn):
pk=open(fn,'rb')
r = pickle.load(pk)
pk.close()
return r
if __name__ == '__main__':
call=defaultdict(list)
gate=defaultdict(list)
if len(sys.argv) >= 2:
fn=sys.argv[1]
if len(sys.argv) == 3:
lines = int(sys.argv[2])
else:
lines = None
if len(sys.argv) >= 2:
if 'telnet:' in fn:
mycall,aprspass = open('.aprspass').readline().split()
out = open('telnet.txt','a')
if fn == 'telnet:':
fn = 'telnet:noam.aprs2.net:14580'
print 'connecting to North America tier 2 server'
t = re.match('^telnet:(?P<host>[^:]+):(?P<port>.*)$',fn)
tn = Telnet(t.group('host'),t.group('port'))
out.write('{} '.format(int(time.time())))
out.write(tn.read_until('\r\n'))
tn.write('user {} pass {} filter q/rR/I\n'.format(mycall,aprspass))
while True:
try:
l = tn.read_until('\n')
tm = int(time.time())
out.write('{} '.format(tm))
out.write(l)
parse(l,clock=tm)
except:
break
if lines:
lines -= 1
if lines <= 0:
print 'logging out'
logout(tn)
out.close()
break
else:
if '.bz2' in fn:
f=bz2.BZ2File(fn)
else:
f=open(fn)
for l in f:
t = re.match('^(?P<time>[\d\.]+) (?P<packet>.*$)',l)
if t:
parse(t.group('packet'),clock=float(t.group('time')))
else:
print '?Eh: {}'.format(l)
print 'if you want to pickle the call list do this:'
print "dump(call,'call.pickled')"
else:
print 'loading pickled call list'
call = load('call.pickled')
invert()
(pct,badgates) = checkall()