-
Notifications
You must be signed in to change notification settings - Fork 4
/
eve-to-gns3-converter.py
77 lines (63 loc) · 2.62 KB
/
eve-to-gns3-converter.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
import argparse
import os
import pathlib
from topology import Topology
def get_arguments():
"""
Creates an argument parser
Returns:
parsed ArgumentParser object
"""
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-f', '--src_topology_file',
help='specify source UNL/EVE topology *.unl file',
type=argparse.FileType('r'))
group.add_argument('-s', '--src_dir',
help='specify source folder containing *.unl files')
parser.add_argument('-d', '--dst_dir', default='dst/',
help='specify destination folder for resulting files')
parser.add_argument('-v', '--verbose', help='increase output verbosity',
action='store_true')
parser.add_argument('-c', '--console_start_port',
help='specify a starting port for console connections, default is 5000',
type=int, default=5000)
parser.add_argument('--l2_iol_image',
help='Specify path to L2 IOL image')
parser.add_argument('--l3_iol_image',
help='Specify path to L3 IOL image')
args = parser.parse_args()
return args
def convert_topology(src_topology_file, args, dst_dir):
# if src_dir is not None:
# path = pathlib.Path(src_dir)
# dst_dir = args.dst_dir / path.relative_to(*path.parts[:1])
# else:
# dst_dir = args.dst_dir
topology = Topology(src_topology_file, args, dst_dir)
topology.write_configs()
topology.write_gns_topology_json()
def main():
args = get_arguments()
if args.src_topology_file:
with args.src_topology_file as f:
src_topology_file = f.read()
convert_topology(src_topology_file, args, args.dst_dir)
elif args.src_dir:
count = 0
for dir_name, _, files in os.walk(args.src_dir):
relative_dir = os.path.relpath(dir_name, args.src_dir)
dst_dir = os.path.join(args.dst_dir, relative_dir)
for filename in files:
if filename.endswith(".unl"):
full_path = os.path.join(dir_name, filename)
print(f'Parsing {full_path}')
with open(full_path) as file:
src_topology_file = file.read()
convert_topology(src_topology_file, args, dst_dir)
count += 1
if not count:
raise FileNotFoundError("No *.unl files have been found.")
if __name__ == '__main__':
main()