Browse Source

ok

pull/2/head
guanghongwei 10 years ago
parent
commit
78b55f981c
  1. 6
      jumpserver.py
  2. 47
      webroot/AutoSa/AutoSa/views.py
  3. 37
      webroot/AutoSa/templates/downFile.html

6
jumpserver.py

@ -164,7 +164,7 @@ def sth_select(username='', ip=''):
"""if username: return password elif ip return port"""
db, cursor = connect_db(db_user, db_password, db_db, db_host, db_port)
if username:
cursor.execute('select password from %s where username="%s"' % (user_table, username))
cursor.execute('select ldap_password from %s where username="%s"' % (user_table, username))
try:
password = cursor.fetchone()[0]
except IndexError:
@ -292,7 +292,7 @@ if __name__ == '__main__':
sys.exit()
else:
connect_one(username, option)
except (BaseException, Exception):
#except IndexError:
#except (BaseException, Exception):
except IndexError:
print "Exit."
sys.exit()

47
webroot/AutoSa/AutoSa/views.py

@ -901,20 +901,55 @@ def upFile(request):
for chunk in upload_file.chunks():
f.write(chunk)
f.close()
asset = Assets.objects.get(ip=host, None)
asset = Assets.objects.get(ip=host)
if asset:
port = asset.port
jm = PyCrypt(key)
user = User.objects.get(username)
t = paramiko.Transport(host, port)
t.connect(username=username, password=jm.decrypt(user.password))
user = User.objects.get(username=username)
t = paramiko.Transport((host, port))
t.connect(username=username, password=jm.decrypt(user.ldap_password))
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(filename, path)
sftp.put(filename, '%s/%s' % (path, upload_file.name))
msg = u'上传成功,位于 %s主机,位置 %s.' % (host, path)
return HttpResponse('save %s Ok, size %s' % (upload_file.name, upload_file.size))
return render_to_response('info.html', {'msg': msg})
else:
return render_to_response('info.html', {'error': u"上传失败"})
return render_to_response('upFile.html',
{'username': username},
context_instance=RequestContext(request))
@login_required
def downFile(request):
username = request.session.get('username')
if request.method == 'POST':
host = request.POST.get('host')
path = request.POST.get('path')
download_dir = '/tmp/download/%s' % username
download_file = '%s/%s' % (download_dir, os.path.basename(path))
if not os.path.exists(download_dir):
os.makedirs(download_dir)
asset = Assets.objects.get(ip=host)
jm = PyCrypt(key)
port = asset.port
user = User.objects.get(username=username)
t = paramiko.Transport((host, port))
t.connect(username=username, password=jm.decrypt(user.ldap_password))
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(path, download_file)
if os.path.isfile(download_file):
f = open(download_file)
data = f.read()
f.close()
response = HttpResponse(data, mimetype='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(path)
return response
return render_to_response('downFile.html',
{'username': username},
context_instance=RequestContext(request))

37
webroot/AutoSa/templates/downFile.html

@ -0,0 +1,37 @@
{% extends 'base.html' %}
{% block content %}
<form class="form-horizontal" enctype="multipart/form-data" role="form" method="post" action="" >
<fieldset >
<legend>{{ username }} -- 下载文件</legend>
{% if error %}
<div class="alert alert-danger">
{{ error }}
</div>
{% endif %}
{% if msg %}
<div class="alert alert-success">
{{ msg }}
</div>
{% endif %}
<input type="text" name="username" value="{{ username }}" hidden="hidden">
<div class="form-group">
<label for="host" class="col-sm-2 control-label">服务器IP<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="text" class="form-control" id="host" name="host" placeholder="IP">
</div>
</div>
<div class="form-group">
<label for="path" class="col-sm-2 control-label">文件path<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="text" class="form-control" id="path" name="path" placeholder="文件绝对位置">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button class="btn btn-primary">下载</button>
</div>
</div>
</fieldset>
</form>
{% endblock %}
Loading…
Cancel
Save