-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh-exec.py
304 lines (251 loc) · 9.46 KB
/
ssh-exec.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
'''
Author: James Simpson
Version: 1.1
This script will allow the user to execute multiple commands against multiple
SSH enabled devices.
In its current state, it's used for connecting to Cisco switches / routers,
but with a bit of tweaking could be applied to any number of other devices.
The output can either be displayed in your terminal screen, written to a file,
or sent via email (with a bit of extra configuration)
Uses the following modules:
Paramiko to handle SSH
Getpass for secure password entry
Time to allow for send delay
Readline to prevent input errors
Re for regex matches
Socket to handle socket exceptions
Smtplib to handle email
OS to remove temporary file created for email output
'''
import paramiko
import getpass
import time
import readline
import re
import socket
import smtplib
import os
############################
# #
# FUNCTIONS #
# #
############################
#Function to open a user-specified file and extract a list of hosts
def get_devicenames():
hosts = []
#Loop to ensure that the script doesn't crash out if it can't find the specified file.
while True:
try:
filename = raw_input("Which file contains your host list: ")
openfile = open(filename, 'r')
#The specific error we're most likely to see is "IOError: [Errno 2]"
except IOError, reason:
print "Error: %s" % reason
continue
#This tells the script to continue as normal if no IOError is raised, as the input has been accepted.
else:
break
print "Reading hosts from \'%s\'" % filename
for line in openfile:
line = line.replace('\n','')
line = line.replace('\r','')
hosts.append(line)
return hosts
#Create a list of commands from user input
def get_commands(prompt):
commands = []
while True:
command = raw_input(prompt)
if command == "":
break
else:
commands.append(command)
return commands
#Called by "process_screen" function to assist with processing commands.
def print_command(command, console, device):
length = len(command)
print command
print "*" * 10
console.send("\n")
console.send(command+"\n")
time.sleep(4)
output = console.recv(4096)
try:
start = output.index(command)
output = output.replace(device + '#', ' ' * 13)
output = output.replace(command, ' ' * length)
print output[start:]
except ValueError, reason:
streason = str(reason)
if "substring not found" in streason:
print "Couldn't see your command anywhere in the output - could be a slow or congested link?"
else:
print "There was some kind of error: %s" % reason
#Called by "process_file" function to assist with processing commands.
def write_command(command, console, device, openfile):
length = len(command)
openfile.write(command + "\n")
openfile.write("*" * 10 + "\n")
console.send("\n")
console.send(command+"\n")
time.sleep(4)
output = console.recv(4096)
try:
start = output.index(command)
output = output.replace(device + '#', ' ' * 13)
output = output.replace(command, ' ' * length)
openfile.write(output[start:] + "\n")
except ValueError, reason:
streason = str(reason)
if "substring not found" in streason:
openfile.write("Couldn't see your command anywhere in the output - could be a slow or congested link?\n")
else:
openfile.write("There was some kind of error: %s\n" % reason)
#Used to process commands when output method is set to "screen"
def process_screen(devicenames, username, password, commands):
for device in devicenames:
print '-' * 10
print device
print '-' * 10
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#try / except loop - prevents script from aborting due to socket error.
try:
client.connect(device, username=username, password=password, timeout=5, look_for_keys=False)
#print "SSH connection established to %s" % device
except (socket.error, paramiko.AuthenticationException), reason:
print "Error: %s" % reason
continue
try:
#Invoke remote shell
console = client.invoke_shell()
#Enter enable mode using provided credentials
console.send("en \n")
console.send(str(password)+"\n")
#Set terminal length to 0
console.send("terminal length 0\n")
time.sleep(1)
#Execute commands
for command in commands:
print_command(command, console, device)
#Close connection
console.close()
#Catch socket errors
except socket.error, reason:
print "%s, %s" % (reason, device)
#Used to process commands when output method is set to "file", or called by 'process_email'
def process_file(devicenames, username, password, commands):
print "Working..."
openfile = open(filename, 'a')
for device in devicenames:
openfile.write('-' * 10 + "\n")
openfile.write(device + "\n")
openfile.write('-' * 10 + "\n")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#try / except loop - prevents script from aborting due to socket error.
try:
client.connect(device, username=username, password=password, timeout=5, look_for_keys=False)
#print "SSH connection established to %s" % device
except (socket.error, paramiko.AuthenticationException), reason:
openfile.write("Error: %s\n" % reason)
continue
try:
#Invoke remote shell
console = client.invoke_shell()
#Enter enable mode using provided credentials
console.send("en \n")
console.send(str(password)+"\n")
#Set terminal length to 0
console.send("terminal length 0\n")
time.sleep(1)
#Execute commands
for command in commands:
write_command(command, console, device, openfile)
#Close connection
console.close()
#Catch socket errors
except socket.error, reason:
print "%s, %s" % (reason, device)
openfile.write("Error: %s\n" % reason)
#Used to process commands when output method is set to "email"
def process_email(devicenames, username, password, commands):
process_file(devicenames, username, password, commands)
openfile = open("tempforemail", 'r')
TO = []
while True:
address = raw_input("Enter recipient addresses, press enter when finished: ")
if address == "":
break
else:
TO.append(address)
FROM = "Command.Results@Routers"
SUBJECT = "Example Subject"
TEXT = openfile.read()
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
sendmail(FROM, TO, message)
openfile.close()
os.remove("tempforemail")
#Function used to obtain the user's output method of choice (screen, file, or email)
def get_output_method(prompt):
while True:
method = raw_input(prompt)
if not re.match ("^screen$|^file$|^email$", method):
print ("Please enter \"screen\", \"email\" or \"file\"")
continue
elif method == "file":
filename = raw_input("Filename to write to: ")
break
elif method == "email":
filename = "tempforemail"
break
else:
filename = ""
break
return method, filename
#Function used for sending mail via an unauthenticated or open SMTP relay
def sendmail(FROM, TO, message):
server = smtplib.SMTP('your.open.relay')
server.sendmail(FROM, TO, message)
server.quit()
#Function used for sending mail via an SMTP relay requiring SSL
def sendmail_ssl(FROM, TO, message):
server = smtplib.SMTP_SSL('ssl.smtp.server:465')
server.login('username' 'password')
server.sendmail(FROM, TO, message)
server.quit()
############################
# #
# VARIABLES #
# #
############################
devicenames = get_devicenames()
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
commands = get_commands("Enter Commands, press enter when finished: ")
method, filename = get_output_method("Output to screen, to file, or via email: ")
############################
# #
# EXECUTION #
# #
############################
# Open the "tempforemail" file ready for writing. Script will truncate the file if it exists.
f = open("tempforemail", 'w')
f.close()
if method == "screen":
process_screen(devicenames, username, password, commands)
print "Script has completed successfully!"
elif method == "file":
process_file(devicenames, username, password, commands)
print "Output written to %s" % filename
elif method == "email":
process_email(devicenames, username, password, commands)
print "Email sent!"
else:
print "Some kind of error, output method set to incorrect value."