2015-01-09 13:55:44 -05: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()
|
|
|
|
|
2017-10-24 11:11:31 -04:00
|
|
|
class UnknownFieldsException(Exception):
|
|
|
|
def __init__(self, fields):
|
|
|
|
Exception.__init__(self, "unknown tshark fields %s" % ','.join(fields))
|
|
|
|
self.fields = fields
|
2015-01-09 13:55:44 -05:00
|
|
|
|
|
|
|
_tshark_filter_arg = '-Y'
|
|
|
|
|
2017-10-24 11:11:31 -04:00
|
|
|
def _run_tshark(filename, filter, display=None, wait=True):
|
2015-01-30 08:28:59 -05:00
|
|
|
global _tshark_filter_arg
|
|
|
|
|
2015-01-30 09:23:26 -05: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 17:12:38 -05:00
|
|
|
time.sleep(0.1)
|
2015-01-09 13:55:44 -05:00
|
|
|
|
|
|
|
try:
|
2019-03-15 06:10:37 -04:00
|
|
|
arg = ["tshark", "-r", filename,
|
|
|
|
_tshark_filter_arg, filter]
|
2015-01-09 13:55:44 -05: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 17:38:52 -05:00
|
|
|
stderr=subprocess.PIPE)
|
2019-01-24 02:45:41 -05:00
|
|
|
except Exception as e:
|
2015-01-09 13:55:44 -05:00
|
|
|
logger.info("Could run run tshark check: " + str(e))
|
|
|
|
cmd = None
|
|
|
|
return None
|
|
|
|
|
2016-12-24 17:38:52 -05:00
|
|
|
output = cmd.communicate()
|
2019-02-02 06:05:16 -05:00
|
|
|
out = output[0].decode(errors='ignore')
|
|
|
|
out1 = output[1].decode()
|
2015-01-09 13:55:44 -05:00
|
|
|
res = cmd.wait()
|
|
|
|
if res == 1:
|
2017-10-24 11:11:31 -04:00
|
|
|
errmsg = "Some fields aren't valid"
|
2019-02-02 06:05:16 -05:00
|
|
|
if errmsg in out1:
|
|
|
|
errors = out1.split('\n')
|
2017-10-24 11:11:31 -04:00
|
|
|
fields = []
|
|
|
|
collect = False
|
|
|
|
for f in errors:
|
|
|
|
if collect:
|
|
|
|
f = f.strip()
|
|
|
|
if f:
|
|
|
|
fields.append(f)
|
|
|
|
continue
|
|
|
|
if errmsg in f:
|
|
|
|
collect = True
|
|
|
|
continue
|
|
|
|
raise UnknownFieldsException(fields)
|
2015-01-09 13:55:44 -05:00
|
|
|
# remember this for efficiency
|
|
|
|
_tshark_filter_arg = '-R'
|
|
|
|
arg[3] = '-R'
|
|
|
|
cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
|
|
|
|
stderr=open('/dev/null', 'w'))
|
2019-02-02 06:05:16 -05:00
|
|
|
out = cmd.communicate()[0].decode()
|
2015-01-09 13:55:44 -05:00
|
|
|
cmd.wait()
|
2018-10-16 11:21:07 -04:00
|
|
|
if res == 2:
|
2019-02-02 06:05:16 -05:00
|
|
|
if "tshark: Neither" in out1 and "are field or protocol names" in out1:
|
|
|
|
errors = out1.split('\n')
|
2018-10-16 11:21:07 -04:00
|
|
|
fields = []
|
|
|
|
for f in errors:
|
|
|
|
if f.startswith("tshark: Neither "):
|
|
|
|
f = f.split(' ')[2].strip('"')
|
|
|
|
if f:
|
|
|
|
fields.append(f)
|
|
|
|
continue
|
|
|
|
raise UnknownFieldsException(fields)
|
2015-01-09 13:55:44 -05:00
|
|
|
|
|
|
|
return out
|
2017-10-24 11:11:31 -04:00
|
|
|
|
|
|
|
def run_tshark(filename, filter, display=None, wait=True):
|
|
|
|
if display is None: display = []
|
|
|
|
try:
|
2019-12-26 17:02:05 -05:00
|
|
|
return _run_tshark(filename, filter.replace('wlan_mgt', 'wlan'),
|
|
|
|
[x.replace('wlan_mgt', 'wlan') for x in display],
|
|
|
|
wait)
|
2019-01-24 02:45:41 -05:00
|
|
|
except UnknownFieldsException as e:
|
2017-10-24 11:11:31 -04:00
|
|
|
all_wlan_mgt = True
|
|
|
|
for f in e.fields:
|
|
|
|
if not f.startswith('wlan_mgt.'):
|
|
|
|
all_wlan_mgt = False
|
|
|
|
break
|
|
|
|
if not all_wlan_mgt:
|
|
|
|
raise
|
2019-12-26 17:02:05 -05:00
|
|
|
return _run_tshark(filename, filter, display, wait)
|
2018-10-17 05:33:11 -04:00
|
|
|
|
|
|
|
def run_tshark_json(filename, filter):
|
2019-03-15 06:10:37 -04:00
|
|
|
arg = ["tshark", "-r", filename,
|
|
|
|
_tshark_filter_arg, filter]
|
2018-10-17 05:33:11 -04:00
|
|
|
arg.append('-Tjson')
|
|
|
|
arg.append('-x')
|
|
|
|
try:
|
|
|
|
cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2019-01-24 02:45:41 -05:00
|
|
|
except Exception as e:
|
2018-10-17 05:33:11 -04:00
|
|
|
logger.info("Could run run tshark: " + str(e))
|
|
|
|
return None
|
|
|
|
output = cmd.communicate()
|
2019-02-02 06:05:16 -05:00
|
|
|
out = output[0].decode()
|
2018-10-17 05:33:11 -04:00
|
|
|
res = cmd.wait()
|
|
|
|
return out
|