Skip to content
Extraits de code Groupes Projets
Valider 3c8d249c rédigé par François De Keersmaeker's avatar François De Keersmaeker
Parcourir les fichiers

Updated -r option

parent 88d455ab
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -45,7 +45,7 @@ It will be created if it doesn't exist. ...@@ -45,7 +45,7 @@ It will be created if it doesn't exist.
* `-h`, `--help`: show help message and exit * `-h`, `--help`: show help message and exit
* `-d`, `--dry-run`: don't write the output PCAP file (but still write the CSV log file) * `-d`, `--dry-run`: don't write the output PCAP file (but still write the CSV log file)
* `-r`, `--random-range`: upper bound for the random range, which will select for each packet if it will be edited or not. In practice, each packet will be edited with a probability of `1/(r+1)`. Default: `0` (edit all packets). * `-r`, `--random-range`: upper bound for the random range, which will select for each packet if it will be edited or not. In practice, each packet will be edited with a probability of `1/r`. Must be a strictly positive integer. Default: `1` (edit all packets).
* `-n`, `--packet-number`: index of the packet to edit, starting from 1. Can be specified multiple times. If this is used, only the specified packets will be edited, and no random editing will be performed. * `-n`, `--packet-number`: index of the packet to edit, starting from 1. Can be specified multiple times. If this is used, only the specified packets will be edited, and no random editing will be performed.
......
...@@ -13,6 +13,24 @@ from scapy.contrib import coap, igmp, igmpv3 ...@@ -13,6 +13,24 @@ from scapy.contrib import coap, igmp, igmpv3
from packet.Packet import Packet from packet.Packet import Packet
def strictly_positive_int(value: any) -> int:
"""
Custom argparse type for a strictly positive integer value.
:param value: argument value to check
:return: argument as integer if it is strictly positive
:raises argparse.ArgumentTypeError: if argument does not represent a strictly positive integer
"""
try:
ivalue = int(value)
except ValueError:
raise argparse.ArgumentTypeError(f"{value} does not represent an integer.")
else:
if ivalue < 1:
raise argparse.ArgumentTypeError(f"{value} does not represent a strictly positive integer.")
return ivalue
if __name__ == "__main__": if __name__ == "__main__":
# Script-related variables # Script-related variables
...@@ -32,11 +50,14 @@ if __name__ == "__main__": ...@@ -32,11 +50,14 @@ if __name__ == "__main__":
# Positional arguments: input PCAP file # Positional arguments: input PCAP file
parser.add_argument("input_pcaps", metavar="pcap", type=str, nargs="+", help="Input PCAP files.") parser.add_argument("input_pcaps", metavar="pcap", type=str, nargs="+", help="Input PCAP files.")
# Optional flag: -d / --dry-run # Optional flag: -d / --dry-run
parser.add_argument("-d", "--dry-run", action="store_true", help="Dry run: do not write output PCAP file.") parser.add_argument("-d", "--dry-run", action="store_true",
help="Dry run: do not write output PCAP file.")
# Optional flag: -r / --random-range # Optional flag: -r / --random-range
parser.add_argument("-r", "--random-range", type=int, default=0, help="Upper bound for random range.") parser.add_argument("-r", "--random-range", type=strictly_positive_int, default=1,
help="Upper bound for random range (not included). Must be a strictly positive integer. Default: 1 (edit each packet).")
# Optional flag: -n / --packet-number # Optional flag: -n / --packet-number
parser.add_argument("-n", "--packet-number", type=int, action="append", help="Index of the packet to edit, starting form 1. Can be specifed multiple times.") parser.add_argument("-n", "--packet-number", type=int, action="append",
help="Index of the packet to edit, starting form 1. Can be specifed multiple times.")
# Parse arguments # Parse arguments
args = parser.parse_args() args = parser.parse_args()
...@@ -82,7 +103,7 @@ if __name__ == "__main__": ...@@ -82,7 +103,7 @@ if __name__ == "__main__":
for packet in packets: for packet in packets:
# Choose randomly if we edit this packet # Choose randomly if we edit this packet
if random.randint(0, args.random_range) != 0: if random.randrange(0, args.random_range) != 0:
# Packet won't be edited # Packet won't be edited
# Go to next packet # Go to next packet
i += 1 i += 1
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter