@@ -32,18 +32,18 @@ def get_denylist():
32
32
return denylist
33
33
34
34
35
- def main (program , ifname , count ):
36
- if not ifname :
35
+ def main (program , options ):
36
+ if not options . ifname :
37
37
raise ValueError ("Empty interface name." )
38
- if any ((c == "/" or c .isspace ()) for c in ifname ):
39
- raise ValueError (f"Interface name '{ ifname } ' contains invalid characters." )
40
- if len (ifname ) > 16 :
41
- raise ValueError (f"Interface name '{ ifname } ' is too long." )
38
+ if any ((c == "/" or c .isspace ()) for c in options . ifname ):
39
+ raise ValueError (f"Interface name '{ options . ifname } ' contains invalid characters." )
40
+ if len (options . ifname ) > 16 :
41
+ raise ValueError (f"Interface name '{ options . ifname } ' is too long." )
42
42
43
43
denylist = get_denylist ()
44
44
45
- if ifname in denylist :
46
- raise ValueError (f"Interface name '{ ifname } ' is denied in denylist." )
45
+ if options . ifname in denylist :
46
+ raise ValueError (f"Interface name '{ options . ifname } ' is denied in denylist." )
47
47
48
48
programs = ["tcpreplay" , "tcpdump" ]
49
49
if program not in programs :
@@ -54,18 +54,18 @@ def main(program, ifname, count):
54
54
]
55
55
56
56
if program == "tcpreplay" :
57
- args .append (f"--intf1={ ifname } " )
57
+ args .append (f"--intf1={ options . ifname } " )
58
58
args .append ("-" )
59
59
60
60
if program == "tcpdump" :
61
61
args .append ("-n" )
62
- args .append (f"--interface={ ifname } " )
62
+ args .append (f"--interface={ options . ifname } " )
63
63
args .append ("-w" )
64
64
args .append ("-" )
65
65
66
- if count :
66
+ if options . count :
67
67
args .append ("-c" )
68
- args .append (str (count ))
68
+ args .append (str (options . count ))
69
69
70
70
try :
71
71
os .execvp (args [0 ], args )
@@ -76,12 +76,20 @@ def main(program, ifname, count):
76
76
if __name__ == "__main__" :
77
77
parser = argparse .ArgumentParser ()
78
78
parser .add_argument ("-d" , "--debug" , action = "store_true" , default = False , help = "enable debug mode" )
79
- parser .add_argument ("program" , type = str , help = "program to run, either tcpreplay or tcpdump" )
80
- parser .add_argument ("interface" , type = str , help = "interface name" )
81
- parser .add_argument ("count" , nargs = "?" , type = int , default = None , help = "amount of frames to capture while recording" )
79
+ subparsers = parser .add_subparsers (dest = "program" , help = "program to run" )
80
+
81
+ # tcpdump
82
+ tcpdump_parser = subparsers .add_parser ("tcpdump" )
83
+ tcpdump_parser .add_argument ("ifname" , type = str , help = "interface name" )
84
+ tcpdump_parser .add_argument ("count" , type = int , default = None , help = "amount of frames to capture while recording" )
85
+
86
+ # tcpreplay
87
+ tcpreplay_parser = subparsers .add_parser ("tcpreplay" )
88
+ tcpreplay_parser .add_argument ("ifname" , type = str , help = "interface name" )
89
+
82
90
args = parser .parse_args ()
83
91
try :
84
- main (args .program , args . interface , args . count )
92
+ main (args .program , args )
85
93
except Exception as e : # pylint: disable=broad-except
86
94
if args .debug :
87
95
import traceback
0 commit comments