]> git.proxmox.com Git - mirror_frr.git/blame - tests/topotests/lib/scapy_sendpkt.py
tests: Fix for frr-bot style issues
[mirror_frr.git] / tests / topotests / lib / scapy_sendpkt.py
CommitLineData
002e6825
CH
1#!/usr/bin/env python3
2# -*- coding: utf-8 eval: (blacken-mode 1) -*-
3#
4# July 29 2021, Christian Hopps <chopps@labn.net>
5#
6# Copyright (c) 2021, LabN Consulting, L.L.C. ("LabN")
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in all
16# copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24# SOFTWARE.
25#
26import argparse
27import logging
28import re
29import sys
30
31from scapy.all import conf, srp
32
33conf.verb = 0
34
35
36def main():
37 parser = argparse.ArgumentParser()
38 parser.add_argument("-i", "--interface", help="interface to send packet on.")
39 parser.add_argument("-I", "--imports", help="scapy symbols to import")
40 parser.add_argument(
41 "-t", "--timeout", type=float, default=2.0, help="timeout for reply receipts"
42 )
43 parser.add_argument("pktdef", help="scapy packet definition to send")
44 args = parser.parse_args()
45
46 if args.imports:
47 i = args.imports.replace("\n", "").strip()
48 if not re.match("[a-zA-Z0-9_ \t,]", i):
49 logging.critical('Invalid imports specified: "%s"', i)
50 sys.exit(1)
51 exec("from scapy.all import " + i, globals(), locals())
52
53 ans, unans = srp(eval(args.pktdef), iface=args.interface, timeout=args.timeout)
54 if not ans:
55 sys.exit(2)
56 for pkt in ans:
57 print(pkt.answer.show(dump=True))
58
59
60if __name__ == "__main__":
61 main()