2015-01-09 19:55:44 +01:00
|
|
|
#
|
|
|
|
# tshark module - refactored from test_scan.py
|
|
|
|
#
|
|
|
|
# Copyright (c) 2014, Qualcomm Atheros, Inc.
|
|
|
|
# Copyright (c) 2015, Intel Mobile Communications GmbH
|
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
|
|
|
import time
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
_tshark_filter_arg = '-Y'
|
|
|
|
|
2015-01-30 15:23:26 +01:00
|
|
|
def run_tshark(filename, filter, display=None, wait=True):
|
2015-01-30 14:28:59 +01:00
|
|
|
global _tshark_filter_arg
|
|
|
|
|
2015-01-30 15:23:26 +01:00
|
|
|
if wait:
|
|
|
|
# wait a bit to make it more likely for wlantest sniffer to have
|
|
|
|
# captured and written the results into a file that we can process here
|
2015-11-26 23:12:38 +01:00
|
|
|
time.sleep(0.1)
|
2015-01-09 19:55:44 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
arg = [ "tshark", "-r", filename,
|
2015-01-30 14:28:59 +01:00
|
|
|
_tshark_filter_arg, filter ]
|
2015-01-09 19:55:44 +01:00
|
|
|
if display:
|
|
|
|
arg.append('-Tfields')
|
|
|
|
for d in display:
|
|
|
|
arg.append('-e')
|
|
|
|
arg.append(d)
|
|
|
|
else:
|
|
|
|
arg.append('-V')
|
|
|
|
cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
|
2016-12-24 23:38:52 +01:00
|
|
|
stderr=subprocess.PIPE)
|
2015-01-09 19:55:44 +01:00
|
|
|
except Exception, e:
|
|
|
|
logger.info("Could run run tshark check: " + str(e))
|
|
|
|
cmd = None
|
|
|
|
return None
|
|
|
|
|
2016-12-24 23:38:52 +01:00
|
|
|
output = cmd.communicate()
|
|
|
|
out = output[0]
|
2015-01-09 19:55:44 +01:00
|
|
|
res = cmd.wait()
|
|
|
|
if res == 1:
|
2016-12-24 23:38:52 +01:00
|
|
|
if "Some fields aren't valid" in output[1]:
|
|
|
|
raise Exception("Unknown tshark field")
|
2015-01-09 19:55:44 +01:00
|
|
|
# remember this for efficiency
|
|
|
|
_tshark_filter_arg = '-R'
|
|
|
|
arg[3] = '-R'
|
|
|
|
cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
|
|
|
|
stderr=open('/dev/null', 'w'))
|
|
|
|
out = cmd.communicate()[0]
|
|
|
|
cmd.wait()
|
|
|
|
|
|
|
|
return out
|