forked from utkarshmahar/CCIE-RS-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgns3.py
More file actions
80 lines (59 loc) · 2.22 KB
/
Copy pathgns3.py
File metadata and controls
80 lines (59 loc) · 2.22 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
import yaml
from LabConnection import *
import threading
import requests,json,time,yaml
class Gns3:
def __init__(self):
with open('yamlfiles/console.yaml') as f:
o = yaml.safe_load(f)
self.gns3_host = "http://"+o["gns3_host_ip"]+":3080/"
def start(self,device):
url = self.gns3_host+"v2/projects/"
headers = {"Accept":"application/json","Content-Type":"application/json"}
with open('topology.gns3') as f:
json_output = json.loads(f.read())
project_id = json_output["project_id"]
url = url + project_id
for node in json_output['topology']['nodes']:
if node['name'] == device:
node_id = node['node_id']
print "starting ",device
response = requests.request("POST",url+"/nodes/"+node_id+"/start")
def stop(self,device):
url = self.gns3_host+ "v2/projects/"
headers = {"Accept":"application/json","Content-Type":"application/json"}
with open('topology.gns3') as f:
json_output = json.loads(f.read())
project_id = json_output["project_id"]
url = url + project_id
for node in json_output['topology']['nodes']:
if node['name'] == device:
node_id = node['node_id']
print "stopping ", device
response = requests.request("POST",url+"/nodes/"+node_id+"/stop")
def stop_all(self):
threads = []
with open('topology.gns3') as f:
json_output = json.loads(f.read())
project_id = json_output["project_id"]
for node in json_output['topology']['nodes']:
threads.append(threading.Thread(target=self.stop,args=(node['name'],)))
for t in threads:
t.start()
for t in threads:
t.join()
def start_all(self):
threads = []
with open('topology.gns3') as f:
json_output = json.loads(f.read())
project_id = json_output["project_id"]
for node in json_output['topology']['nodes']:
threads.append(threading.Thread(target=self.start,args=(node['name'],)))
for t in threads:
t.start()
for t in threads:
t.join()
def reset_lab(self):
print "reseting lab"
self.stop_all()
self.start_all()