tshark.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #
  2. # tshark module - refactored from test_scan.py
  3. #
  4. # Copyright (c) 2014, Qualcomm Atheros, Inc.
  5. # Copyright (c) 2015, Intel Mobile Communications GmbH
  6. #
  7. # This software may be distributed under the terms of the BSD license.
  8. # See README for more details.
  9. import time
  10. import subprocess
  11. import logging
  12. logger = logging.getLogger()
  13. _tshark_filter_arg = '-Y'
  14. def run_tshark(filename, filter, display=None, wait=True):
  15. global _tshark_filter_arg
  16. if wait:
  17. # wait a bit to make it more likely for wlantest sniffer to have
  18. # captured and written the results into a file that we can process here
  19. time.sleep(0.1)
  20. try:
  21. arg = [ "tshark", "-r", filename,
  22. _tshark_filter_arg, filter ]
  23. if display:
  24. arg.append('-Tfields')
  25. for d in display:
  26. arg.append('-e')
  27. arg.append(d)
  28. else:
  29. arg.append('-V')
  30. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  31. stderr=subprocess.PIPE)
  32. except Exception, e:
  33. logger.info("Could run run tshark check: " + str(e))
  34. cmd = None
  35. return None
  36. output = cmd.communicate()
  37. out = output[0]
  38. res = cmd.wait()
  39. if res == 1:
  40. if "Some fields aren't valid" in output[1]:
  41. raise Exception("Unknown tshark field")
  42. # remember this for efficiency
  43. _tshark_filter_arg = '-R'
  44. arg[3] = '-R'
  45. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  46. stderr=open('/dev/null', 'w'))
  47. out = cmd.communicate()[0]
  48. cmd.wait()
  49. return out