forked from mapp-john/ise-password-change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPASSWORD_CHANGE.py
235 lines (206 loc) · 7.83 KB
/
PASSWORD_CHANGE.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
234
235
import os
import re
import sys
import time
import yaml
import shutil
import jinja2
import socket
import random
import logging
import netmiko
from threading import Thread
import queue as queue
from ISEFunctions import *
from EmailModule import emailHTMLWithRenamedAttachment
def COMMANDS( login_username,login_password,username,new_password,counter,device_type,devices,deviceList,outputList,CIMC):
while not deviceList.empty():
device = deviceList.get_nowait()
if not CIMC:
try:
# Connection Break
counter = len(devices)-deviceList.qsize()
print('\n['+str(counter)+'] Connecting to: '+device+'\n')
outputList.put('\n['+str(counter)+'] Connecting to: '+device+'\n')
# Connection Handler
connection = netmiko.ConnectHandler(ip=device, device_type=device_type, username=login_username, password=login_password, global_delay_factor=9)
# Change WebGui Password
output = connection.send_command_timing('application reset-passwd ise '+username)
if 'Password reset is only possible' in output:
print('Error')
else:
connection.send_command_timing(new_password)
connection.send_command_timing(new_password)
# Change CLI Password
connection.send_command_timing('conf t')
connection.send_command_timing('username '+username+' password plain '+new_password+' role admin')
connection.send_command_timing('end')
connection.send_command_timing('wr mem')
connection.send_command_timing('\n\n\n\n')
outputList.put(('!\n['+str(counter)+'] PASSWORD CHANGE: PASSWORD CHANGE COMPLETED - '+device+'\n!'))
try:
connection.disconnect()
except OSError:
pass
except:
outputList.put(('\n!'+'\n!'+'\n['+str(counter)+'] PASSWORD CHANGE: DISCONNECT ERROR - '+device+'\n!'+'\n!'))
except: # exceptions as exceptionOccured:
outputList.put(('\n!'+'\n!'+'\n['+str(counter)+'] PASSWORD CHANGE: CONNECTION ERROR - '+device+'\n!'+'\n!'))
else:
try:
# Connection Break
counter = len(devices)-deviceList.qsize()
print('\n['+str(counter)+'] Connecting to: '+device+'\n')
outputList.put('\n['+str(counter)+'] Connecting to: '+device+'\n')
# Connection Handler
connection = netmiko.ConnectHandler(ip=device, device_type=device_type, username=login_username, password=login_password, global_delay_factor=9)
# Find Local user index
output = connection.send_command_timing('show user')
for line in output.strip().splitlines():
line = line.split()
if username == line[1]:
user_index = line[0]
if not user_index:
outputList.put(('\n['+str(counter)+'] PASSWORD CHANGE: USER ('+username+') NOT FOUND - '+device+'\n!'))
raise Exception
# Change Password
connection.send_command_timing('scope user '+user_index)
output = connection.send_command_timing('set password')
if 'Please enter password' not in output:
outputList.put(('\n['+str(counter)+'] PASSWORD CHANGE: ERROR CHANGING PASSWORD - '+device+'\n!'))
raise Exception
else:
connection.send_command_timing(new_password)
connection.send_command_timing(new_password)
# Change CLI Password
connection.send_command_timing('commit')
connection.send_command_timing('exit')
connection.send_command_timing('exit')
outputList.put(('!\n['+str(counter)+'] PASSWORD CHANGE: PASSWORD CHANGE COMPLETED - '+device+'\n!'))
try:
connection.disconnect()
except OSError:
pass
except:
outputList.put(('\n!'+'\n!'+'\n['+str(counter)+'] PASSWORD CHANGE: DISCONNECT ERROR - '+device+'\n!'+'\n!'))
except: # exceptions as exceptionOccured:
outputList.put(('\n!'+'\n!'+'\n['+str(counter)+'] PASSWORD CHANGE: CONNECTION ERROR - '+device+'\n!'+'\n!'))
outputList.put(None)
return
def script(form,configSettings):
# Pull variables from web form
deployment = form['deployment']
login_username = form['login_username']
login_password = form['login_password']
username = form['username']
new_password = form['new_password']
email = form['email']
# Netmiko Device Type
device_type = 'linux'
NAC_NA = [
'USNAC1.domain.com',
'USNAC2.domain.com',
]
NAC_EU = [
'EUNAC1.domain.com',
'EUNAC2.domain.com',
]
NAC_AP = [
'APNAC1.domain.com',
'APNAC2.domain.com',
]
NDA = [
'USNAC3.domain.com',
'USNAC4.domain.com',
]
NAC_NA_CIMC = [
'USNAM1.domain.com',
'USNAM2.domain.com',
]
NAC_EU_CIMC = [
'EUCIMC1.domain.com',
'EUCIMC1.domain.com',
]
NAC_AP_CIMC = [
'APCIMC1.domain.com',
'APCIMC2.domain.com',
]
NDA_CIMC = [
'USCIMC3.domain.com',
'USCIMC4.domain.com',
]
CIMC = False
if deployment == 'NAC_NA':
devices = NAC_NA
elif deployment == 'NAC_EU':
devices = NAC_EU
elif deployment == 'NAC_AP':
devices = NAC_AP
elif deployment == 'NDA':
devices = NDA
elif deployment == 'NAC_NA_CIMC':
devices = NAC_NA_CIMC
CIMC = True
elif deployment == 'NAC_EU_CIMC':
devices = NAC_EU_CIMC
CIMC = True
elif deployment == 'NAC_AP_CIMC':
devices = NAC_AP_CIMC
CIMC = True
elif deployment == 'NDA_CIMC':
devices = NDA_CIMC
CIMC = True
# Define Threading Queues
NUM_THREADS = 20
deviceList = queue.Queue()
outputList = queue.Queue()
if len(devices) < NUM_THREADS:
NUM_THREADS = len(devices)
for line in devices:
deviceList.put(line.strip())
# Random Generated Output File
outputDirectory = ''
outputFileName = ''
for i in range(6):
outputDirectory += chr(random.randint(97,122))
outputDirectory += '/'
if not os.path.exists(outputDirectory):
os.makedirs(outputDirectory)
for i in range(6):
outputFileName += chr(random.randint(97,122))
outputFileName += '.txt'
counter = 0
# loop for devices
for i in range(NUM_THREADS):
Thread(target=COMMANDS, args=(login_username,login_password,username,new_password,counter,device_type,devices,deviceList,outputList,CIMC)).start()
time.sleep(1)
with open(outputFileName,'w') as outputFile:
numDone = 0
while numDone < NUM_THREADS:
result = outputList.get()
if result is None:
numDone += 1
else:
outputFile.write(result)
##############################
# Email Out Result
#
subject = 'Results for ISE Password Change'
html = """
<html>
<body>
<h1>Output from ISE Password Change Script</h1>
</body>
</html>
"""
attachmentfile = outputFileName
attachmentname = 'results.csv'
#
From = 'NAC Migration <NAC_Migration@domain.com>'
#
emailHTMLWithRenamedAttachment(email,subject,html,attachmentfile,attachmentname,From)
# Delete Directory and Output File
if os.path.exists(outputDirectory):
shutil.rmtree(outputDirectory,ignore_errors=True)
os.remove(outputFileName)
return