File: scapyPlugin.py

package info (click to toggle)
linux 5.10.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,182,916 kB
  • sloc: ansic: 19,488,074; asm: 263,676; sh: 73,873; makefile: 44,685; perl: 34,640; python: 32,386; cpp: 6,070; yacc: 4,755; lex: 2,742; awk: 1,214; ruby: 25; sed: 5
file content (50 lines) | stat: -rw-r--r-- 1,515 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3

import os
import signal
from string import Template
import subprocess
import time
from TdcPlugin import TdcPlugin

from tdc_config import *

try:
    from scapy.all import *
except ImportError:
    print("Unable to import the scapy python module.")
    print("\nIf not already installed, you may do so with:")
    print("\t\tpip3 install scapy==2.4.2")
    exit(1)

class SubPlugin(TdcPlugin):
    def __init__(self):
        self.sub_class = 'scapy/SubPlugin'
        super().__init__()

    def post_execute(self):
        if 'scapy' not in self.args.caseinfo:
            if self.args.verbose:
                print('{}.post_execute: no scapy info in test case'.format(self.sub_class))
            return

        # Check for required fields
        scapyinfo = self.args.caseinfo['scapy']
        scapy_keys = ['iface', 'count', 'packet']
        missing_keys = []
        keyfail = False
        for k in scapy_keys:
            if k not in scapyinfo:
                keyfail = True
                missing_keys.add(k)
        if keyfail:
            print('{}: Scapy block present in the test, but is missing info:'
                .format(self.sub_class))
            print('{}'.format(missing_keys))

        pkt = eval(scapyinfo['packet'])
        if '$' in scapyinfo['iface']:
            tpl = Template(scapyinfo['iface'])
            scapyinfo['iface'] = tpl.safe_substitute(NAMES)
        for count in range(scapyinfo['count']):
            sendp(pkt, iface=scapyinfo['iface'])