0001
0002
0003 import os
0004 import signal
0005 from string import Template
0006 import subprocess
0007 import time
0008 from TdcPlugin import TdcPlugin
0009
0010 from tdc_config import *
0011
0012 try:
0013 from scapy.all import *
0014 except ImportError:
0015 print("Unable to import the scapy python module.")
0016 print("\nIf not already installed, you may do so with:")
0017 print("\t\tpip3 install scapy==2.4.2")
0018 exit(1)
0019
0020 class SubPlugin(TdcPlugin):
0021 def __init__(self):
0022 self.sub_class = 'scapy/SubPlugin'
0023 super().__init__()
0024
0025 def post_execute(self):
0026 if 'scapy' not in self.args.caseinfo:
0027 if self.args.verbose:
0028 print('{}.post_execute: no scapy info in test case'.format(self.sub_class))
0029 return
0030
0031
0032 lscapyinfo = self.args.caseinfo['scapy']
0033 if type(lscapyinfo) != list:
0034 lscapyinfo = [ lscapyinfo, ]
0035
0036 for scapyinfo in lscapyinfo:
0037 scapy_keys = ['iface', 'count', 'packet']
0038 missing_keys = []
0039 keyfail = False
0040 for k in scapy_keys:
0041 if k not in scapyinfo:
0042 keyfail = True
0043 missing_keys.append(k)
0044 if keyfail:
0045 print('{}: Scapy block present in the test, but is missing info:'
0046 .format(self.sub_class))
0047 print('{}'.format(missing_keys))
0048
0049 pkt = eval(scapyinfo['packet'])
0050 if '$' in scapyinfo['iface']:
0051 tpl = Template(scapyinfo['iface'])
0052 scapyinfo['iface'] = tpl.safe_substitute(NAMES)
0053 for count in range(scapyinfo['count']):
0054 sendp(pkt, iface=scapyinfo['iface'])