-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchconf.py
109 lines (90 loc) · 3.52 KB
/
switchconf.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
import concurrent.futures
import csv
import threading
import time
import os
import cisco2950t
class Batch:
def __init__(self, offset, size):
self.offset = offset
self.size = size
self.batch_status = [''] * size
self.oks = [False] * size
self.lock = threading.Lock()
self.preflight_executor = \
concurrent.futures.ThreadPoolExecutor(max_workers=size)
with open('switches.csv', newline='') as f:
reader = csv.reader(f)
config = [row for row in reader]
self.config = config[offset: offset + size]
self.telnet_host = '192.168.0.250'
self.telnet_base_port = 2000
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
self.snmp_community = config['2950t']['snmp_community']
self.old_enable_password = config['2950t']['old_enable_password']
self.enable_password = config['2950t']['enable_password']
self.access_password = config['2950t']['access_password']
def configure(self):
def _preflight(telnet_host, telnet_port, enable_password):
index = telnet_port - 2000 - 1
while True:
try:
cisco2950t.flash(
telnet_host=telnet_host,
telnet_port=telnet_port,
password=self.old_enable_password)
with self.lock:
self.batch_status[index] = \
f'{index + 1: >2}: {self.config[index][0]} - Preflight succeeded'
# print(index)
self.oks[index] = True
break
except Exception as e:
with self.lock:
self.batch_status[index] = f'{index + 1: >2}: {self.config[index][0]} - ' + str(e)
time.sleep(5)
for n in range(self.size):
self.preflight_executor.submit(
_preflight,
self.telnet_host,
self.telnet_base_port + n + 1,
self.enable_password,
)
with self.lock:
self.batch_status[n] = f'{n + 1: >2}: {self.config[n][0]} - Processing'
while not all(self.oks):
os.system("printf '\033c'")
print(self)
time.sleep(10)
time.sleep(5)
os.system("printf '\033c'")
for n, c in enumerate(self.config):
n = n+1
print(f'{n: >2} - {c[0]: <25} mgnt={c[1]: <15} gw={c[2]: <15}')
if n == 8:
print('-----------------------------------------------')
print('\n--\nWait while writing configuration to switches...')
for n in range(self.size):
self.preflight_executor.submit(
cisco2950t.configure,
self.telnet_host,
self.telnet_base_port + n + 1,
self.config[n][0], # hostname
self.config[n][1], # management
self.config[n][2], # gateway
self.snmp_community,
self.enable_password,
self.access_password,
)
self.preflight_executor.shutdown(wait=True)
def __str__(self):
return '\n'.join(self.batch_status)
if __name__ == '__main__':
import sys
# start index in switches.csv, 0, 16, 32 etc if size is 16
start_index = int(sys.argv[1]) * 16
# Set size of batch
b = Batch(start_index, 16)
b.configure()