-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.py
359 lines (303 loc) · 13.4 KB
/
code.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import web
from web import form
import paramiko
#paramiko.util.log_to_file('/tmp/filename.log')
# install web.py
# use pip install or easy_install web.py
# or download source
# wget http://webpy.org/static/web.py-0.37.tar.gz
# tar xvf web.py-0.37.tar.gz
# cd web.py-0.37
# python setup.py install
##############
# run this file:
# python code.py
##############
# visit this page on port 8080
# http://23.21.167.60:8080/
render = web.template.render('templates/')
urls = ('/', 'index')
app = web.application(urls, globals())
myform = form.Form(
form.Dropdown('mydrop', [('value1', 'first server'), ('value2', 'second server'), ('value3', 'third server')]),
form.Textbox("middle_server"),
form.Textbox("middle_port",
form.regexp('\d+', 'Usually port 22')),
form.Textbox('middle_user'),
form.Password('middle_password'),
form.Textbox("second_server"),
form.Textbox("second_port"),
form.Textbox('second_user'),
form.Password('second_password'),
form.Textbox("third_server"),
form.Textbox("third_port"),
form.Textbox('third_user'),
form.Password('third_password'),
form.Textbox('exec_command'),
form.File('myfile'),
form.Textbox('file_send_location'),
form.Textbox('file_receive'),
form.Textbox('file_receive_location'))
#form.Textbox('file_send'),
def myfirstconnect(middle_server, middle_port, middle_user, middle_password, mycommand, file_send, file_rec, rece_f_loc, send_f_loc):
myreturn=''
try:
import paramiko
paramiko.util.log_to_file('/tmp/filename.log')
except:
print "import error"
try:
proxy_client = paramiko.SSHClient()
proxy_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
except:
print "error1 "
myline= "proxy_client.connect('%s', port=%s, username='%s', password='%s')" % (middle_server, int(middle_port), middle_user, middle_password)
try:
exec myline
except:
print "error myfirstconnect"
try:
sftp = proxy_client.open_sftp()
sftp.put(file_send, send_f_loc)
sftp.close()
if send_f_loc:
myreturn+="file uploaded successfully to "+send_f_loc
myreturn+="\n############################################\n"
except:
print "error while put %s %s " % (file_send, send_f_loc)
put_error = "error while sending file %s to %s " % (file_send, send_f_loc)
myreturn += '\n'+put_error+'\n\n\n'
try:
(sshin1, sshout1, ssherr1) = proxy_client.exec_command(mycommand)
mytext=sshout1.read()
mytext_error=ssherr1.read()
if mytext:
myreturn += mytext
elif mytext_error:
myreturn += mytext_error
except:
myreturn +="\n error while executing the command on remote host \n"
try:
sftp = proxy_client.open_sftp()
sftp.get(file_rec, rece_f_loc)
sftp.close()
if rece_f_loc:
myreturn+="\n\n###########################################################\n\n"
myreturn+="download your file from http://23.21.167.60:8000/"+rece_f_loc.split('/')[-1]
except:
print "error while get %s %s " % (file_rec, rece_f_loc)
get_error = "error while receiving file from %s to %s " % (file_rec, rece_f_loc)
myreturn += '\n'+get_error+'\n'
return myreturn
def mysecondconnect(middle_server, middle_port, middle_user, middle_password, second_server, second_port, second_user, second_password, mycommand, file_send, file_rec, rece_f_loc, send_f_loc):
myreturn=''
try:
import paramiko
except:
print "import error"
try:
proxy_client = paramiko.SSHClient()
proxy_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
except:
print "error1 "
myline= "proxy_client.connect('%s', port=%s, username='%s', password='%s')" % (middle_server, int(middle_port), middle_user, middle_password)
try:
exec myline
except:
print "error myfirstconnect in function mysecondconnect"
transport = proxy_client.get_transport()
try:
myline2="dest_addr = ('%s', %d)" % (second_server, int(second_port))
print myline2
exec myline2
except:
print "error in myline2"
try:
local_addr = ('127.0.0.1', 1234)
channel = transport.open_channel("direct-tcpip", dest_addr, local_addr)
last_server = paramiko.SSHClient()
last_server.set_missing_host_key_policy(paramiko.AutoAddPolicy())
myline3="last_server.connect(hostname='localhost', username='%s', password='%s', sock=channel)" % (second_user, second_password)
exec myline3
print myline3
except:
print "error in myline3"
try:
sftp = last_server.open_sftp()
sftp.put(file_send, send_f_loc)
sftp.close()
if send_f_loc:
myreturn+="file uploaded successfully to "+send_f_loc
myreturn+="\n############################################\n"
except:
print "error while put %s %s " % (file_send, send_f_loc)
put_error = "error while sending file %s to %s " % (file_send, send_f_loc)
myreturn += '\n'+put_error+'\n\n\n'
try:
(sshin1, sshout1, ssherr1) = last_server.exec_command(mycommand)
mytext=sshout1.read()
mytext_error=ssherr1.read()
if mytext:
myreturn += mytext
elif mytext_error:
myreturn += mytext_error
except:
myreturn += "\n error myfirstconnect in function mysecondconnect \n"
try:
sftp = last_server.open_sftp()
sftp.get(file_rec, rece_f_loc)
sftp.close()
if rece_f_loc:
myreturn+="\n\n###########################################################\n\n"
myreturn+="download your file from http://23.21.167.60:8000/"+rece_f_loc.split('/')[-1]
except:
print "error while get %s %s " % (file_rec, rece_f_loc)
get_error = "error while receiving file from %s to %s " % (file_rec, rece_f_loc)
myreturn += '\n'+get_error+'\n'
return myreturn
def mythirdconnect(middle_server, middle_port, middle_user, middle_password, second_server, second_port, second_user, second_password, third_server, third_port, third_user, third_password, mycommand, file_send, file_rec, rece_f_loc, send_f_loc):
myreturn=''
try:
import paramiko
except:
print "import error"
try:
proxy_client = paramiko.SSHClient()
proxy_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
except:
print "error1 "
myline= "proxy_client.connect('%s', port=%s, username='%s', password='%s')" % (middle_server, int(middle_port), middle_user, middle_password)
try:
exec myline
except:
print "error myfirstconnect in function mysecondconnect"
exit
transport = proxy_client.get_transport()
try:
myline2="dest_addr = ('%s', %d)" % (second_server, int(second_port))
print myline2
exec myline2
except:
print "error in myline2"
try:
local_addr = ('127.0.0.1', 1234)
channel = transport.open_channel("direct-tcpip", dest_addr, local_addr)
last_server = paramiko.SSHClient()
last_server.set_missing_host_key_policy(paramiko.AutoAddPolicy())
myline3="last_server.connect(hostname='localhost', username='%s', password='%s', sock=channel)" % (second_user, second_password)
exec myline3
print myline3
except:
print "error in myline3"
###
transport1 = last_server.get_transport()
try:
myline2="dest_addr1 = ('%s', %d)" % (third_server, int(third_port))
print myline2
exec myline2
except:
print "error in myline2"
try:
local_addr = ('127.0.0.1', 1234)
channel1 = transport1.open_channel("direct-tcpip", dest_addr1, local_addr)
etim_server = paramiko.SSHClient()
etim_server.set_missing_host_key_policy(paramiko.AutoAddPolicy())
myline3="etim_server.connect(hostname='localhost', username='%s', password='%s', sock=channel1)" % (third_user, third_password)
exec myline3
print myline3
except:
print "error in myline3"
try:
sftp = etim_server.open_sftp()
sftp.put(file_send, send_f_loc)
sftp.close()
if send_f_loc:
myreturn+="file uploaded successfully to "+send_f_loc
myreturn+="\n############################################\n"
except:
print "error while put %s %s " % (file_send, send_f_loc)
put_error = "error while sending file %s to %s " % (file_send, send_f_loc)
myreturn += '\n'+put_error+'\n\n\n'
try:
(sshin1, sshout1, ssherr1) = etim_server.exec_command(mycommand)
mytext=sshout1.read()
mytext_error=ssherr1.read()
if mytext:
myreturn += mytext
elif mytext_error:
myreturn += mytext_error
except:
print "error while executing the command on remote host"
myreturn += "\n error while executing the command on remote host \n"
try:
sftp = etim_server.open_sftp()
sftp.get(file_rec, rece_f_loc)
sftp.close()
if rece_f_loc:
myreturn+="\n\n###########################################################\n\n"
myreturn+="download your file from http://23.21.167.60:8000/"+rece_f_loc.split('/')[-1]
except:
print "error while get %s %s " % (file_rec, rece_f_loc)
get_error = "error while receiving file from %s to %s " % (file_rec, rece_f_loc)
myreturn += '\n'+get_error+'\n'
return myreturn
class index:
def GET(self):
form = myform()
# make sure you create a copy of the form by calling it (line above)
# Otherwise changes will appear globally
return render.formtest(form)
def POST(self):
toshow=''
form = myform()
if not form.validates():
return render.formtest(form)
else:
# form.d.boe and form['boe'].value are equivalent ways of
# extracting the validated arguments from the form.
middle_server=form['middle_server'].value.encode('ascii', 'ignore') or None
middle_port=int(form['middle_port'].value.encode('ascii', 'ignore')) or None
middle_user=form['middle_user'].value.encode('ascii', 'ignore') or None
middle_password=form['middle_password'].value or None
mycommand=form['exec_command'].value.encode('ascii', 'ignore') or None
file_send = file_rec = rece_f_loc = send_f_loc = None
second_server=form['second_server'].value.encode('ascii', 'ignore') or None
second_port=form['second_port'].value.encode('ascii', 'ignore') or None
second_user=form['second_user'].value.encode('ascii', 'ignore') or None
second_password=form['second_password'].value or None
third_server=form['third_server'].value.encode('ascii', 'ignore') or None
third_port=form['third_port'].value.encode('ascii', 'ignore') or None
third_user=form['third_user'].value.encode('ascii', 'ignore') or None
third_password=form['third_password'].value or None
user_option=form['mydrop'].value
file_rec=form['file_receive'].value.encode('ascii', 'ignore') or None
rece_f_loc=form['file_receive_location'].value.encode('ascii', 'ignore') or None
#file_send=form['file_send'].value.encode('ascii', 'ignore') or None
send_f_loc=form['file_send_location'].value.encode('ascii', 'ignore') or None
x = web.input(myfile={})
filedir = '/tmp'
if 'myfile' in x:
filepath=x.myfile.filename.replace('\\','/')
filename=filepath.split('/')[-1]
file_send = '/tmp/' + filename
try:
fout = open(filedir +'/'+ filename,'w')
fout.write(x.myfile.file.read())
fout.close()
except IOError:
pass
print middle_server.encode('ascii','ignore'), middle_port, middle_user, middle_password, mycommand, file_send, file_rec, rece_f_loc, send_f_loc
if user_option == 'value3':
toshow+="############output of command ############## on server "+third_server+" ####### \n\n"
toshow+=mythirdconnect(middle_server, middle_port , middle_user , middle_password , second_server, second_port, second_user, second_password, third_server, third_port, third_user, third_password, mycommand , file_send, file_rec, rece_f_loc, send_f_loc)
elif user_option == 'value2':
toshow+="############output of command ############## on server "+second_server+" ####### \n\n"
toshow+=mysecondconnect(middle_server, middle_port , middle_user , middle_password , second_server, second_port, second_user, second_password, mycommand , file_send, file_rec, rece_f_loc, send_f_loc)
else:
toshow+="############output of command ############## on server "+middle_server+" ####### \n\n"
toshow+=myfirstconnect(middle_server, middle_port , middle_user , middle_password , mycommand , file_send, file_rec, rece_f_loc, send_f_loc)
#return "Grrreat success! boe: %s, bax: %s" % (form['middle_server'].value, form['middle_port'].value)
return toshow
if __name__=="__main__":
web.internalerror = web.debugerror
app.run()