44
44
with open ('ip_list.txt' , 'r' ) as ip :
45
45
ip_list = ip .read ().split ('\n ' )
46
46
47
- #graceful handling of keyboardInterruption
47
+ #graceful keyboardInterrupt
48
48
def signal_handler (signal , frame ):
49
49
print ("\n # Program exiting gracefully" )
50
50
socket .close (0 )
@@ -56,7 +56,7 @@ def signal_handler(signal, frame):
56
56
def getStatus ():
57
57
for ip in ip_list :
58
58
try :
59
- print ('# Trying to log in to %s' % (ip ))
59
+ print ('# Trying to login to %s' % (ip ))
60
60
#create ssh session with active directory credentials
61
61
ssh = paramiko .SSHClient ()
62
62
ssh .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
@@ -119,16 +119,16 @@ def checkHost():
119
119
print ('# Can not find out hostname, script closed' )
120
120
ssh .close ()
121
121
elif re .search (r'Dell' , brandOutput ) or re .search (r'dell' , brandOutput ):
122
- print ('# Brand of device is Dell' )
123
- try :
124
- ssh .slave .send ('show run | find hostname\n ' )
125
- time .sleep (5 )
126
- hostOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
127
- hostname = hostOutput .split ('\n ' )[1 ].split (' ' )[1 ]
128
- print ('# Hostname of device is %s' % (hostname .strip ()))
129
- except :
130
- print ('# Can not find out hostname, script closed' )
131
- ssh .close ()
122
+ print ('# Brand of device is Dell' )
123
+ try :
124
+ ssh .slave .send ('show run | find hostname\n ' )
125
+ time .sleep (5 )
126
+ hostOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
127
+ hostname = hostOutput .split ('\n ' )[1 ].split (' ' )[1 ]
128
+ print ('# Hostname of device is %s' % (hostname .strip ()))
129
+ except :
130
+ print ('# Can not find out hostname, script closed' )
131
+ ssh .close ()
132
132
checkHost ()
133
133
134
134
#inter status
@@ -154,102 +154,116 @@ def writeStat():
154
154
writeStat ()
155
155
ssh .close ()
156
156
157
+ except TimeoutError :
158
+ print ('# Timeout: Can not connect to device. Check network reachability.' )
159
+ print ('' )
160
+ ssh .close ()
161
+
157
162
except :
158
- #create ssh session with local credentials
159
- ssh = paramiko .SSHClient ()
160
- ssh .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
161
- ssh .connect (
162
- ip ,
163
- port = port ,
164
- username = locUsername ,
165
- password = locPassword ,
166
- look_for_keys = False ,
167
- allow_agent = False )
168
- ssh .slave = ssh .invoke_shell ()
169
- print ('# Logged in with local credentials' )
163
+ print ('# Authentication failed: Can not login with selected credentials.' )
164
+ print ('# Trying to login with local credentials...' )
165
+ try :
166
+ #create ssh session with active directory credentials
167
+ ssh = paramiko .SSHClient ()
168
+ ssh .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
169
+ ssh .connect (
170
+ ip ,
171
+ port = port ,
172
+ username = locUsername ,
173
+ password = locPassword ,
174
+ look_for_keys = False ,
175
+ allow_agent = False )
176
+ ssh .slave = ssh .invoke_shell ()
177
+ print ('# Logged in with local credentials' )
170
178
171
- #check ena
172
- def checkEna ():
173
- time .sleep (3 )
174
- ssh .slave .send ('\n ' )
175
- time .sleep (1 )
176
- enaOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
177
- if '#' in enaOutput :
178
- print ('# Logged in privilege mode' )
179
- else :
180
- print ('# Trying to log in privilege mode' )
181
- ssh .slave .send ('ena\n ' )
182
- time .sleep (1 )
183
- ssh .slave .send (enaPassword )
179
+ #check ena
180
+ def checkEna ():
181
+ time .sleep (3 )
184
182
ssh .slave .send ('\n ' )
185
183
time .sleep (1 )
186
184
enaOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
187
185
if '#' in enaOutput :
188
186
print ('# Logged in privilege mode' )
189
187
else :
190
- print ('# Can not log in privilege mode, script closed' )
191
- ssh .close ()
192
- checkEna ()
188
+ print ('# Trying to log in privilege mode' )
189
+ ssh .slave .send ('ena\n ' )
190
+ time .sleep (1 )
191
+ ssh .slave .send (locPassword )
192
+ ssh .slave .send ('\n ' )
193
+ time .sleep (1 )
194
+ enaOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
195
+ if '#' in enaOutput :
196
+ print ('# Logged in privilege mode' )
197
+ else :
198
+ print ('# Can not log in privilege mode, script closed' )
199
+ ssh .close ()
200
+ checkEna ()
193
201
194
- #disable paging
195
- def disPage ():
196
- ssh .slave .send ('term len 0\n ' )
197
- print ('# Paging disabled' )
198
- ssh .slave .recv (9999 ).decode ('UTF-8' )
199
- time .sleep (1 )
200
- disPage ()
202
+ #disable paging
203
+ def disPage ():
204
+ ssh .slave .send ('term len 0\n ' )
205
+ print ('# Paging disabled' )
206
+ ssh .slave .recv (9999 ).decode ('UTF-8' )
207
+ time .sleep (1 )
208
+ disPage ()
201
209
202
- #lookup for hostname
203
- def checkHost ():
204
- global hostname
205
- ssh .slave .send ('show version\n ' )
206
- time .sleep (1 )
207
- brandOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
208
- if re .search (r'Cisco' , brandOutput ) or re .search (r'cisco' , brandOutput ):
209
- print ('# Brand of device is Cisco' )
210
- try :
211
- ssh .slave .send ('show run | include hostname\n ' )
212
- time .sleep (5 )
213
- hostOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
214
- hostname = hostOutput .split ('\n ' )[1 ].split (' ' )[1 ]
215
- print ('# Hostname of device is %s' % (hostname .strip ()))
216
- except :
217
- print ('# Can not find out hostname, script closed' )
218
- ssh .close ()
219
- elif re .search (r'Dell' , brandOutput ) or re .search (r'dell' , brandOutput ):
220
- print ('# Brand of device is Dell' )
221
- try :
222
- ssh .slave .send ('show run | find hostname\n ' )
223
- time .sleep (5 )
224
- hostOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
225
- hostname = hostOutput .split ('\n ' )[1 ].split (' ' )[1 ]
226
- print ('# Hostname of device is %s' % (hostname .strip ()))
227
- except :
228
- print ('# Can not find out hostname, script closed' )
229
- ssh .close ()
230
- checkHost ()
210
+ #lookup for hostname
211
+ def checkHost ():
212
+ global hostname
213
+ ssh .slave .send ('show version\n ' )
214
+ time .sleep (1 )
215
+ brandOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
216
+ if re .search (r'Cisco' , brandOutput ) or re .search (r'cisco' , brandOutput ):
217
+ print ('# Brand of device is Cisco' )
218
+ try :
219
+ ssh .slave .send ('show run | include hostname\n ' )
220
+ time .sleep (5 )
221
+ hostOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
222
+ hostname = hostOutput .split ('\n ' )[1 ].split (' ' )[1 ]
223
+ print ('# Hostname of device is %s' % (hostname .strip ()))
224
+ except :
225
+ print ('# Can not find out hostname, script closed' )
226
+ ssh .close ()
227
+ elif re .search (r'Dell' , brandOutput ) or re .search (r'dell' , brandOutput ):
228
+ print ('# Brand of device is Dell' )
229
+ try :
230
+ ssh .slave .send ('show run | find hostname\n ' )
231
+ time .sleep (5 )
232
+ hostOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
233
+ hostname = hostOutput .split ('\n ' )[1 ].split (' ' )[1 ]
234
+ print ('# Hostname of device is %s' % (hostname .strip ()))
235
+ except :
236
+ print ('# Can not find out hostname, script closed' )
237
+ ssh .close ()
238
+ checkHost ()
231
239
232
- #inter status
233
- def interStat ():
234
- global statusOutput
235
- ssh .slave .send ('show inter status\n ' )
236
- print ('# Lookup for inter status' )
237
- time .sleep (2 )
238
- statusOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
239
- interStat ()
240
+ #inter status
241
+ def interStat ():
242
+ global statusOutput
243
+ ssh .slave .send ('show inter status\n ' )
244
+ print ('# Lookup for inter status' )
245
+ time .sleep (2 )
246
+ statusOutput = ssh .slave .recv (9999 ).decode ('UTF-8' )
247
+ interStat ()
240
248
241
- #inter status file
242
- def writeStat ():
243
- print ('# Write output of inter status to %s%s.txt' % (path , hostname .strip ()))
244
- time .sleep (1 )
245
- for line in statusOutput .splitlines ():
246
- statFile = open ('%sInterStatus-%s.txt' % (path , hostname .strip ()), 'a' )
247
- statFile .write (line )
248
- statFile .write ('\n ' )
249
- statFile .close ()
250
- print ('# Inter status output saved to %s%s.txt' % (path , hostname .strip ()))
249
+ #inter status file
250
+ def writeStat ():
251
+ print ('# Write output of inter status to %s%s.txt' % (path , hostname .strip ()))
252
+ time .sleep (1 )
253
+ for line in statusOutput .splitlines ():
254
+ statFile = open ('%sInterStatus-%s.txt' % (path , hostname .strip ()), 'a' )
255
+ statFile .write (line )
256
+ statFile .write ('\n ' )
257
+ statFile .close ()
258
+ print ('# Inter status output saved to %s%s.txt' % (path , hostname .strip ()))
259
+ print ('' )
260
+ writeStat ()
261
+ ssh .close ()
262
+
263
+ except :
264
+ print ('# Authentication failed: Can not login with locale credentials.' )
251
265
print ('' )
252
- writeStat ()
253
- ssh . close ()
254
- print ('Script ran successfully, script closed' )
266
+ ssh . close ()
267
+
268
+ print ('Script ran successfully. Script closed. ' )
255
269
getStatus ()
0 commit comments