-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configure.py
130 lines (111 loc) · 4.76 KB
/
Configure.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
from netmiko import ConnectHandler
from itertools import chain
from rich import print as rp
from rich.prompt import Prompt
from jinja2 import FileSystemLoader, Environment
from Network.Devices import Routers, Firewalls, Edge_Routers, Spokes, Switches
# Jinja Templates Directory filepath:
Template_dir =input('Jinja Templates Directory filepath: ')
# Configuring Netflow on Branch routers:
rp(f'[bold cyan]----------Configuring NetFlow----------[/cyan]')
for devices in Spokes.values():
c = ConnectHandler(**devices)
c.enable()
host = c.send_command('show version', use_textfsm=True)[0]['hostname']
server = input('IP address of the Flow Collector: ')
interface = input(f'{host} Netflow source interface: ')
Port = input(f'{host} Netflow UDP-Port: ')
data = {
'Flow_Interface': interface,
'UDP_Port': Port,
'Server_IP': server
}
env = Environment(loader=FileSystemLoader(Template_dir))
template = env.get_template('NetFlow.j2')
commands = template.render(data)
rp(c.send_config_set(commands.splitlines()),'\n')
c.save_config()
c.disconnect()
# Configuring NTP
rp(f'\n[bold cyan]----------Configuring NTP on Network Devices---------[/bold cyan]')
for devices in chain(Firewalls.values(), Routers.values(), Spokes.values(),Edge_Routers.values(),Switches.values()):
c = ConnectHandler(**devices)
c.enable()
host = c.send_command('show version', use_textfsm=True)[0]['hostname']
ntp = input(f'NTP server IP for host {host}: ')
source = input(f'{host} NTP source interface: ')
data = {
'ntp_server': ntp,
'source_intf': source
}
env = Environment(loader=FileSystemLoader(Template_dir))
template = env.get_template('NTP.j2')
commands = template.render(data)
rp(c.send_config_set(commands.splitlines()),'\n')
c.save_config()
c.disconnect()
#Configuring CoPP
rp(f'\n[bold cyan]----------Configuring Edge Routers---------[/bold cyan]')
for devices in chain(Firewalls.values(), Routers.values(), Spokes.values(),Edge_Routers.values(),Switches.values()):
c = ConnectHandler(**devices)
c.enable()
host = c.send_command('show version', use_textfsm=True)[0]['hostname']
eigrp_enabled = input(f'Is EIGRP protocol Running on {host} (Y/N): ')
ospf_enabled = input(f'Is OSPF protocol Running on {host} (Y/N): ')
data = {
'eigrp_enabled': eigrp_enabled,
'ospf_enabled': ospf_enabled
}
env = Environment(loader=FileSystemLoader(Template_dir))
template = env.get_template('CoPP.j2')
commands = template.render(data)
rp(c.send_config_set(commands.splitlines()),'\n')
c.save_config()
c.disconnect()
# Configure Syslog
rp(f'\n[bold cyan]----------Configuring Syslog---------[/bold cyan]')
for devices in chain(Routers.values(), Firewalls.values(),Edge_Routers.values(), Spokes.values(), Switches.values()):
c = ConnectHandler(**devices)
c.enable()
host = c.send_command('show version', use_textfsm=True)[0]['hostname']
commands = ['logging host 192.168.20.254',
'logging monitor informational']
output = c.send_config_set(commands)
rp(host,output, sep='\n')
c.save_config()
c.disconnect()
# COnfigure banner MOTD
rp('[cyan]----------Configuring MOTD banner---------[/cyan]')
for devices in chain(Routers.values(), Firewalls.values(),Edge_Routers.values(), Spokes.values(), Switches.values()):
c = ConnectHandler(**devices)
c.enable()
host = c.send_command('show version',use_textfsm=True)[0]['hostname']
commands = [
'banner login @',
f'{"*"*50}',
f'{" "*13}{host}',
f'{" "*5}Configured using CLI and Python',
f'{" "}Unauthorized access is strictly forbidden',
f'{"*"*50}',
'@']
rp(c.send_config_set(commands),'\n')
c.save_config()
c.disconnect()
# Configure EEM
rp(f'\n[bold cyan]----------Configuring Embedded Event Manager--------[/bold cyan]')
Server_IP = Prompt.ask("[bright_magenta]IP address of the TFTP server: [/]")
for devices in chain(Routers.values(), Firewalls.values(),Edge_Routers.values(), Spokes.values(), Switches.values()):
c = ConnectHandler(**devices)
c.enable()
host = c.send_command('show version', use_textfsm=True)[0]['hostname']
Config_filename = Prompt.ask(f'[magenta]Name of the configuration file for host {host} (with .txt extension): [/]')
data = {
'Server_IP': Server_IP,
'Config_filename': Config_filename
}
env = Environment(loader=FileSystemLoader(Template_dir))
template = env.get_template('EEM.j2')
commands = template.render(data)
rp(c.send_config_set(commands.splitlines()),'\n')
c.save_config()
c.disconnect()