diff options
Diffstat (limited to 'spicedump.py')
-rwxr-xr-x | spicedump.py | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/spicedump.py b/spicedump.py index aac6ff3..9f7c8bb 100755 --- a/spicedump.py +++ b/spicedump.py @@ -3,9 +3,10 @@ import sys from collections import defaultdict from itertools import izip_longest from time import time -from optparse import OptionParser +import argparse import textwrap import logging +import re import pcaputil from proxy import proxy, closeallsockets @@ -68,14 +69,26 @@ class SurfaceStatistics(object): for i, l in enumerate(textwrap.wrap(','.join(map(str, v)), width=40)): yield '%20s: %s' % (k if i == 0 else '', l) +def debug_break(): + import rpdb2; rpdb2.start_embedded_debugger('a') + def spicedump(p, opts, stdscr=None): + show_every_command = True + periodic_hist = False import pcapspice spice = pcapspice.spice_iter(p) hist = Histogram() surface_stat = SurfaceStatistics() last_print = start_time = time() messages = [] - filter = opts.filter + filter_exp = opts.filter + if filter_exp: + if filter_exp[0] == '-': + def filter(result_name, exp=re.compile(filter_exp[1:])): + return exp.match(result_name) + else: + def filter(result_name, exp=re.compile(filter_exp)): + return not exp.match(result_name) if stdscr: stdscr.erase() # replace the "for d in spice:" loop with a select @@ -87,7 +100,7 @@ def spicedump(p, opts, stdscr=None): d = spice.next() result_name = d.msg.data.result_name result_value = d.msg.data.result_value - if filter and result_name not in filter: + if filter_exp and filter(result_name): continue if hasattr(p, 'drop_next'): pass @@ -108,7 +121,9 @@ def spicedump(p, opts, stdscr=None): messages.extend(str(d).split('\n')) old_time, old_count = hist[result_name] hist[result_name] = (cur_time, old_count + 1) - if True or cur_time - last_print > dt: + if show_every_command: + print result_name + if periodic_hist and cur_time - last_print > dt: show(hist.show(), surface_stat.show()) print '\n'.join(messages[-20:]) last_print = cur_time @@ -122,22 +137,24 @@ def fromproxy(stdscr, local_port, remote_addr, opts): return spicedump(p, opts, stdscr=stdscr) if __name__ == '__main__': - parser = OptionParser() - parser.add_option('-p', '--proxy', dest='proxy', help='use proxy', + parser = argparse.ArgumentParser() + parser.add_argument('-p', '--proxy', dest='proxy', help='use proxy', action='store_true') - parser.add_option('-l', '--localport', dest='local_port', help='set proxy local port') - parser.add_option('-r', '--remoteaddr', dest='remote_addr', help='set proxy remote address') - parser.add_option('-v', '--verbose', dest='verbose', action='count', help='verbosity', default=0) - parser.add_option('-c', '--curses', dest='curses', action='store_true', help='use curses') - parser.add_option('-f', '--filter', dest='filter', help='filter messages') - opts, rest = parser.parse_args(sys.argv[1:]) + parser.add_argument('-l', '--local-port', type=int, help='set proxy local port') + parser.add_argument('-H', '--remote-host', default='localhost', help='set proxy remote address') + parser.add_argument('-p', '--remote-port', type=int, required=True, help='set proxy remote address') + parser.add_argument('-v', '--verbose', dest='verbose', action='count', help='verbosity', default=0) + parser.add_argument('-c', '--curses', dest='curses', action='store_true', help='use curses') + parser.add_argument('-f', '--filter', dest='filter', help='filter messages') + parser.add_argument('--record', dest='record', help='TODO: record to file, can be used to playback') + parser.add_argument('--playback', dest='playback', help='TODO: playback a previously recorded file') + opts, rest = parser.parse_known_args(sys.argv[1:]) if opts.verbose >= 2 in sys.argv: logging.basicConfig(filename='spicedump.log', level=logging.DEBUG) print "saving debug log to spicedump.log" if opts.proxy: - local_port = int(opts.local_port) - remote_addr = opts.remote_addr.split(':') - remote_addr = (remote_addr[0], int(remote_addr[1])) + local_port = opts.local_port + remote_addr = (opts.remote_host, opts.remote_port) main = (lambda stdscr, opts, local_port=local_port, remote_addr=remote_addr: fromproxy(stdscr, local_port, remote_addr, opts)) else: |