tshark.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. # wait a bit to make it more likely for wlantest sniffer to have captured
  16. # and written the results into a file that we can process here
  17. time.sleep(1)
  18. try:
  19. arg = [ "tshark", "-r", filename,
  20. _tshark_filter_arg, "wlan.fc.type_subtype == 4",
  21. "-Tfields", ]
  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