-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis.py
234 lines (203 loc) · 7.5 KB
/
apis.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from fastapi import FastAPI, status
from pydantic import BaseModel
from netmiko import ConnectHandler
from Device_list.Devices import Routers
from jinja2 import Environment, FileSystemLoader
app = FastAPI()
'''
CRYPTO REST-API
'''
class Crypto(BaseModel):
policy_number : int
@app.post('/Devices/{Device_ID}/Configure/Cryptography', status_code=status.HTTP_201_CREATED)
def CryptoConfig(Device_ID:str, post:Crypto):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'Policy_Number': str(post.policy_number)}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('Cryptography.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
DHCP REST-API
'''
class DHCPClass(BaseModel):
START_IP : str
END_IP : str
NETWORK : str
NETMASK : str
GATEWAY: str
DHCP_POOL_NAME : str
@app.post('/Devices/{Device_ID}/Configure/DHCP', status_code=status.HTTP_201_CREATED)
def dhcpconfig(Device_ID:str, post:DHCPClass):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
variables = {'START_IP': post.START_IP,'END_IP': post.END_IP,'NETWORK': post.NETWORK,
'NETMASK': post.NETMASK,'GATEWAY': post.GATEWAY, 'DHCP_POOL_NAME':post.DHCP_POOL_NAME}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('DHCP.j2')
commands = template.render(variables)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
NETFLOW REST-API
'''
class NetFlowclass(BaseModel):
Flow_Interface : str
Server_IP : str
UDP_Port : int
@app.post('/Devices/{Device_ID}/Configure/NetFlow', status_code=status.HTTP_201_CREATED)
def dhcpconfig(Device_ID:str, post:NetFlowclass):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'Flow_Interface': post.Flow_Interface, 'Server_IP': post.Server_IP,
'UDP_Port': str(post.UDP_Port)}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('NetFlow.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
INTERFACE REST-API
'''
class Intflass(BaseModel):
Interface_name : str
Description : str
Address : str
Netmask : str
@app.post('/Devices/{Device_ID}/Configure/Interface', status_code=status.HTTP_201_CREATED)
def dhcpconfig(Device_ID:str, post:Intflass):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'Interface_name': post.Interface_name, 'Description': post.Description,
'Address': post.Address, 'Netmask': post.Netmask}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('Interface.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
SPOKE TUNNEL REST-API
'''
class spokeclass(BaseModel):
Tunnel_ID : int
Description : str
Source: str
IP_address: str
Netmask : str
Authentication : str
Network_ID : int
NHS_IP : str
NBMA_IP : str
Tunnel_Key : int
Delay : int
@app.post('/Devices/{Device_ID}/Configure/Interface/Spoke-tunnel', status_code=status.HTTP_201_CREATED)
def dhcpconfig(Device_ID:str, post:spokeclass):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'Tunnel_ID': str(post.Tunnel_ID), 'Description': post.Description,'Source':post.Source,
'IP_address': post.IP_address, 'Netmask': post.Netmask,'Authentication': post.Authentication,
'Network_ID': str(post.Network_ID),
'NHS_IP': str(post.NHS_IP),'NBMA_IP':str(post.NBMA_IP),'Tunnel_key': str(post.Tunnel_Key),
'Delay':str(post.Delay)}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('Spoke_tunnel.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
NAT REST-API
'''
class NATclass(BaseModel):
Inside_intf : str
Outside_intf : str
NATed_IP_address : str
Wildcard_Mask : str
@app.post('/Devices/{Device_ID}/Configure/NAT', status_code=status.HTTP_201_CREATED)
def dhcpconfig(Device_ID:str, post:NATclass):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'Inside_intf':post.Inside_intf,'Outside_intf':post.Outside_intf,
'NATed_IP_address':post.NATed_IP_address,'Wildcard_Mask': post.Wildcard_Mask}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('NAT.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
NTP REST-API
'''
class NTPClass (BaseModel):
ntp_server : str
@app.post('/Devices/{Device_ID}/Configure/NTP', status_code = status.HTTP_201_CREATED)
def ntp_config(post: NTPClass, Device_ID: str):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'ntp_server':post.ntp_server}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('NTP.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
SNMP REST-API
'''
class SNMPclass(BaseModel):
Server_IP : str
SNMP_password : str
@app.post('/Devices/{Device_ID}/Configure/SNMP', status_code = status.HTTP_201_CREATED)
def SNMPconfig(post: SNMPclass, Device_ID: str):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
varibles = {'Server_IP':post.Server_IP, 'SNMP_password': post.SNMP_password}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('SNMP.j2')
commands = template.render(varibles)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()
'''
Zone-based Firewall
'''
class ZBFclass(BaseModel):
protocol_list : list
Inside_intf : list
Outside_intf : list
@app.post('/Devices/{Device_ID}/Configure/ZBF', status_code = status.HTTP_201_CREATED)
def ZBFconfig(post: ZBFclass, Device_ID: str):
device = Routers[Device_ID]
conn = ConnectHandler(**device)
conn.enable()
variables = {'protocol_list': post.protocol_list, 'Inside_intf': post.Inside_intf,
'Outside_intf': post.Outside_intf}
env = Environment(loader=FileSystemLoader('/home/munia/Dual-Hub-DMVPN/Jinja_templates'))
template = env.get_template('ZBF.j2')
commands = template.render(variables)
result = conn.send_config_set(commands.splitlines())
conn.save_config()
conn.disconnect()
return result.splitlines()