tshark.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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):
  15. global _tshark_filter_arg
  16. # wait a bit to make it more likely for wlantest sniffer to have captured
  17. # and written the results into a file that we can process here
  18. time.sleep(1)
  19. try:
  20. arg = [ "tshark", "-r", filename,
  21. _tshark_filter_arg, filter ]
  22. if display:
  23. arg.append('-Tfields')
  24. for d in display:
  25. arg.append('-e')
  26. arg.append(d)
  27. else:
  28. arg.append('-V')
  29. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  30. stderr=open('/dev/null', 'w'))
  31. except Exception, e:
  32. logger.info("Could run run tshark check: " + str(e))
  33. cmd = None
  34. return None
  35. out = cmd.communicate()[0]
  36. res = cmd.wait()
  37. if res == 1:
  38. # remember this for efficiency
  39. _tshark_filter_arg = '-R'
  40. arg[3] = '-R'
  41. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  42. stderr=open('/dev/null', 'w'))
  43. out = cmd.communicate()[0]
  44. cmd.wait()
  45. return out