scapyPlugin.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. import os
  3. import signal
  4. from string import Template
  5. import subprocess
  6. import time
  7. from TdcPlugin import TdcPlugin
  8. from tdc_config import *
  9. try:
  10. from scapy.all import *
  11. except ImportError:
  12. print("Unable to import the scapy python module.")
  13. print("\nIf not already installed, you may do so with:")
  14. print("\t\tpip3 install scapy==2.4.2")
  15. exit(1)
  16. class SubPlugin(TdcPlugin):
  17. def __init__(self):
  18. self.sub_class = 'scapy/SubPlugin'
  19. super().__init__()
  20. def post_execute(self):
  21. if 'scapy' not in self.args.caseinfo:
  22. if self.args.verbose:
  23. print('{}.post_execute: no scapy info in test case'.format(self.sub_class))
  24. return
  25. # Check for required fields
  26. lscapyinfo = self.args.caseinfo['scapy']
  27. if type(lscapyinfo) != list:
  28. lscapyinfo = [ lscapyinfo, ]
  29. for scapyinfo in lscapyinfo:
  30. scapy_keys = ['iface', 'count', 'packet']
  31. missing_keys = []
  32. keyfail = False
  33. for k in scapy_keys:
  34. if k not in scapyinfo:
  35. keyfail = True
  36. missing_keys.append(k)
  37. if keyfail:
  38. print('{}: Scapy block present in the test, but is missing info:'
  39. .format(self.sub_class))
  40. print('{}'.format(missing_keys))
  41. pkt = eval(scapyinfo['packet'])
  42. if '$' in scapyinfo['iface']:
  43. tpl = Template(scapyinfo['iface'])
  44. scapyinfo['iface'] = tpl.safe_substitute(NAMES)
  45. for count in range(scapyinfo['count']):
  46. sendp(pkt, iface=scapyinfo['iface'])