]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/scapy_sendpkt.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / lib / scapy_sendpkt.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 eval: (blacken-mode 1) -*-
3 # SPDX-License-Identifier: MIT
4 #
5 # July 29 2021, Christian Hopps <chopps@labn.net>
6 #
7 # Copyright (c) 2021, LabN Consulting, L.L.C. ("LabN")
8 #
9 import argparse
10 import logging
11 import re
12 import sys
13
14 from scapy.all import conf, srp
15
16 conf.verb = 0
17
18
19 def main():
20 parser = argparse.ArgumentParser()
21 parser.add_argument("-i", "--interface", help="interface to send packet on.")
22 parser.add_argument("-I", "--imports", help="scapy symbols to import")
23 parser.add_argument(
24 "-t", "--timeout", type=float, default=2.0, help="timeout for reply receipts"
25 )
26 parser.add_argument("pktdef", help="scapy packet definition to send")
27 args = parser.parse_args()
28
29 if args.imports:
30 i = args.imports.replace("\n", "").strip()
31 if not re.match("[a-zA-Z0-9_ \t,]", i):
32 logging.critical('Invalid imports specified: "%s"', i)
33 sys.exit(1)
34 exec("from scapy.all import " + i, globals(), locals())
35
36 ans, unans = srp(eval(args.pktdef), iface=args.interface, timeout=args.timeout)
37 if not ans:
38 sys.exit(2)
39 for pkt in ans:
40 print(pkt.answer.show(dump=True))
41
42
43 if __name__ == "__main__":
44 main()