-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.py
executable file
·157 lines (136 loc) · 5.24 KB
/
interact.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
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
#!/usr/bin/env python
import json
from collections import namedtuple
from pprint import pprint
from gns3fy import Gns3Connector
import gns3fy
import requests as requests
import sys
def stop_all_nodes(prj_nodes: list[gns3fy.Node]):
if prj_nodes:
for n in prj_nodes:
print(n.name, n.status, end=" > ")
n.stop()
print(n.status)
def delete_proj(proj: gns3fy.Project):
proj.get()
if proj.status != "closed":
stop_all_nodes(proj.nodes)
proj.close()
proj.delete()
def manage_node(node: gns3fy.Node, proj: gns3fy.Project):
userin = ""
while userin != "exit":
userin = input(
f"{node.name}\np: print info, sa: start node, so: stop node, co: get connected nodes, exit "
)
if userin == "p":
pprint(node)
elif userin == "sa":
node.start()
print(node.status)
elif userin == "so":
node.stop()
print(node.status)
elif userin == "co":
from src.gns3utils import get_connected_nodes
for con_node_id in get_connected_nodes(proj, node.node_id):
conn_node = proj.get_node(node_id=con_node_id)
print(f"{conn_node.name} {conn_node.status}")
def interactive_mode(projects: list[str]):
proj = select_project_by_number(projects)
userin = ""
while userin != "exit":
userin = input(
"n: select a node, nq: select a node quiet, cl: close project, del: delete project "
)
if userin == "n" or userin == "nq":
project_nodes = proj.nodes
if userin != "nq":
project_nodes.sort(key=lambda node: node.name)
for idx_node, node_obj in enumerate(proj.nodes):
print(f"[{idx_node}]: {node_obj.name} {node_obj.status}")
userin = input("node name or number ")
try:
userin = int(userin)
node = project_nodes[userin]
except ValueError:
node = proj.get_node(name=userin)
manage_node(node, proj)
elif userin == "cl":
proj.get_nodes()
stop_all_nodes(proj.nodes)
proj.close()
elif userin == "del":
delete_proj(proj)
exit(0)
def delete_multiple_prjects():
delete_projects_with_name_containing = input("delete_projects_with_name_containing: ")
if delete_projects_with_name_containing.strip() == "":
delete_projects_with_name_containing = "azertyuijhgfds"
for p in proj_list:
if delete_projects_with_name_containing in p[0]:
print(f"will close and delete project {p[0]}")
proj = gns3fy.Project(name=p[0], connector=server_connector)
delete_proj(proj)
def select_project_by_number(projects) -> gns3fy.Project:
proj_idx = int(input("Select project number "))
print(f"{projects[proj_idx]}=")
proj = gns3fy.Project(name=projects[proj_idx][0], connector=server_connector)
proj.get()
print(proj)
return proj
def manage_templates(conn: Gns3Connector):
all_templates_names = [tmpl["name"] for tmpl in conn.get_templates()]
print(all_templates_names)
answer = input("delete all templates? (y/n): ")
if answer.lower() == "y":
for template in all_templates_names:
print(f"deleting {template=} ...")
try:
conn.delete_template(template)
except requests.exceptions.HTTPError as e:
print(f"Skip template {template}: {e}")
# credentials are in ~/.config/GNS3/2.2/gns3_server.conf
server_connector = Gns3Connector(
url="http://localhost:3080",
user="admin",
cred="P1Dwt0FBoFM0yKTx9yGOjzErmXDJIvpxm5AmxqEegTcHWspKd58dykRczak6yzHL",
)
print(server_connector)
new_uuid = "f4aadcdd-8989-47cf-aaf4-8c84bc891618"
# new_params = {
# "name": "api_imported_gns3",
# "path": "%2Fhome%2Fmpoisson%2FGNS3%2Fprojects%2Fimported_gns3"
# }
# new_params = {"name": "python_imported_gns3", "project_id": new_uuid, "path": "/home/mpoisson/GNS3/projects/python_imported_gns3"}
# resp = server_connector.http_call("post", f"http://localhost:3080/v2/projects/{new_uuid}/import", params=new_params)
# pprint(vars(resp))
try:
proj_list = server_connector.projects_summary(is_print=False)
except requests.exceptions.JSONDecodeError as e:
print(f">>>> server_connector, credentials are in ~/.config/GNS3/2.2/gns3_server.conf <<< \n{e}")
exit(1)
print(f"{len(proj_list)} projects")
for idx, proj in enumerate(proj_list):
print(f"{idx}: {proj}")
print("run with -i for interactive program")
possible_arguments = ["-i", "-del", "-stop", "-tmplt"]
if len(sys.argv) != 2:
print(sys.argv)
print(f"you must have 1 argument among {','.join(possible_arguments)}")
exit(1)
if sys.argv[1] == "-i":
interactive_mode(proj_list)
elif sys.argv[1] == "-del":
print("-del mode: delete multiple projects")
delete_multiple_prjects()
elif sys.argv[1] == possible_arguments[2]:
project_to_stop = select_project_by_number(proj_list)
stop_all_nodes(project_to_stop.nodes)
elif sys.argv[1] == possible_arguments[3]:
manage_templates(server_connector)
else:
print(sys.argv)
print(f"you must have 1 argument among {','.join(possible_arguments)}")
exit(1)