-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptGNS.py
More file actions
162 lines (137 loc) · 8.75 KB
/
scriptGNS.py
File metadata and controls
162 lines (137 loc) · 8.75 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import json
import sys
if __name__=="__main__":
if(len(sys.argv)!=2):
print("Invalid arguments. Please run as:")
print("python3 scriptGNS.py <json-config-file-name>")
sys.exit()
with open(sys.argv[1]) as file: # ouverture du json qui décrit le réseau
f = json.load(file)
args = f["routers"]
nb_routeurs = len(args)
for i in range(nb_routeurs): # pour chaque routeur
name = (args[i])["name"] + "_configs_i" + str(i+1) + "_private-config.cfg"
with open(name, "w") as config: # ouverture du script à écrire
config.write("# " + (args[i])["name"] + "\n")
config.write("configure terminal\n")
# Configuration OSPF
if "ospf" in (args[i])["protocols"]:
config.write("router ospf " + (args[i])["process_id"] +"\n")
for j in range(len((args[i])["interfaces"])):
intj = ((args[i])["interfaces"])[j]
# Ne pas mettre les réseaux des vrf dans le routage ospf classique
if "PE" in (args[i])["name"] :
if intj["vrf"]=="" :
config.write(" network "+intj["subnet_address"]+" "+intj["inverted_mask"]+" area "+(args[i])["area"]+"\n")
else :
config.write(" network "+intj["subnet_address"]+" "+intj["inverted_mask"]+" area "+(args[i])["area"]+"\n")
if "P" in (args[i])["name"]:
config.write("router ospf router-id loopback 0\n")
else :
config.write("router-id " + (args[i])["router_id"] +"\n\n")
# Configuration mpls globale au routeur
if "mpls" in (args[i])["protocols"]:
config.write("ip cef\n")
config.write("mpls label range " + (args[i])["label_range"] + "\n\n")
config.write("mpls ldp router-id loopback 0\n")
if "PE" in (args[i])["name"]:
# Configuration des vrf
for j in range(len((args[i])["vrf"])):
vrfj = ((args[i])["vrf"])[j]
config.write("ip vrf "+ vrfj["vrf_name"]+"\n")
if vrfj["rd"]!= "" :
config.write(" rd "+(args[i])["as_bgp"]+":"+vrfj["rd"]+"\n")
for export in vrfj["export"] :
# Permettre aux autres routeurs de router avec cette vrf
config.write(" route-target export "+(args[i])["as_bgp"]+":"+export+"\n")
for import_id in vrfj["import"] :
# Permettre au routeur de router avec cette vrf
config.write(" route-target import "+(args[i])["as_bgp"]+":"+import_id+"\n")
config.write(" exit\n\n")
# Configuration des sessions ospf des vrf
config.write(" router ospf "+((args[i])["process_id"])+str(j+1)+" vrf "+vrfj["vrf_name"]+"\n")
config.write(" redistribute bgp "+(args[i])["as_bgp"]+" subnets \n")
for n in range(len((args[i])["interfaces"])):
intn = ((args[i])["interfaces"])[n]
if intn["vrf"]==str(vrfj["vrf_name"]) :
config.write(" network "+intn["subnet_address"]+" "+intn["inverted_mask"]+" area "+(args[i])["area"]+"\n\n")
# Configuration d'une session bgp
config.write(" router bgp "+(args[i])["as_bgp"]+"\n")
for neighbor in (args[i])["neighbors"]:
config.write(" neighbor "+neighbor+" remote-as "+(args[i])["as_bgp"]+"\n")
config.write(" neighbor "+neighbor+" update-source loopback 0\n\n")
config.write(" address-family vpnv4 \n")
config.write(" neighbor "+neighbor+" activate\n")
config.write(" neighbor "+neighbor+" send-community extended\n")
config.write(" neighbor "+neighbor+" next-hop-self\n")
config.write(" exit-address-family\n\n")
config.write(" address-family ipv4 vrf "+vrfj["vrf_name"] +"\n")
config.write(" redistribute ospf "+((args[i])["process_id"])+str(j+1)+" vrf "+vrfj["vrf_name"] +"\n")
config.write(" no synchronization\n")
config.write(" exit-address-family\n\n")
config.write(" exit\n\n")
if "P" in (args[i])["name"] :
QoS = f["QoS"]
for qos_class in (QoS)["classes"] :
# Définition des classes
config.write("class-map "+qos_class["name"]+"\n")
config.write(" match access-group "+qos_class["access-list"]+"\n")
config.write(" exit\n\n")
config.write("access-list "+qos_class["access-list"]+" permit "+qos_class["name"]+" any any \n\n")
# Définition des politiques
for policy in QoS["policies"]:
if policy["policy_map"]!="IN":
for current_pol_classes in policy["classes"]:
try:
if current_pol_classes[qos_class["name"]] :
current_class = current_pol_classes[qos_class["name"]]
config.write("policy-map "+policy["policy_map"]+"\n")
config.write(" class "+qos_class["name"]+"\n")
# Configuration des exigences à respecter
try:
if current_class["bandwidth"]:
config.write(" bandwidth percent "+current_class["bandwidth"]+"\n")
config.write(" exit\n")
except KeyError:
pass
config.write(" exit\n\n")
except KeyError:
pass
elif "PE" in (args[i])["name"] :
# Configurer le dscp de la classe
config.write("policy-map "+policy["policy_map"]+"\n")
config.write(" class "+qos_class["name"]+"\n")
config.write(" set ip dscp "+qos_class["dscp"]+"\n")
config.write(" exit\n")
config.write(" exit\n\n")
config.write("class-map BESTEFFORT \n")
config.write(" match ip dscp default \n")
config.write(" exit\n\n")
# Configuration des interfaces du routeur
for j in range(len((args[i])["interfaces"])): # pour chaque interface du routeur
intj = (((args[i])["interfaces"])[j]) # intj = tableau qui contient le nom de l'interface, son adresse, etc.
config.write("interface " + intj["interface"]+"\n")
config.write(" ip address " + intj["address"] + " " + intj["prefix_length"] + "\n")
config.write(" no shutdown\n")
# Implémentation d'mpls sur les interfaces à l'intérieur du réseau du provider
if "mpls" in intj["protocols"]:
config.write(" mpls ip \n")
config.write(" mpls mtu override "+intj["mpls_MTU"]+"\n")
# Lien entre les vrf et les adresses ip correspondantes.
if "PE" in (args[i])["name"] :
try :
if intj["ext"]=="true":
# Appliquer la politique d'entrée
config.write(" service-policy input IN\n")
except KeyError:
pass
if intj["vrf"]!= "" :
config.write(" ip vrf forwarding "+intj["vrf"]+"\n")
config.write(" ip address " + intj["address"] + " " + intj["prefix_length"] + "\n")
if "P" in (args[i])["name"] and intj["interface"]!="Loopback0":
QoS = f["QoS"]
for policy in QoS["policies"]:
if policy["policy_map"]!="IN":
config.write(" service-policy output "+policy["policy_map"]+"\n")
config.write(" exit\n\n")
config.write("end\n\n")