pull/2/head
宿召阳 2014-11-07 10:30:13 +08:00
commit 83ac653637
14 changed files with 61 additions and 206 deletions

View File

@ -24,6 +24,8 @@ if not cur_dir:
sys.path.append('%s/webroot/AutoSa/' % cur_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings'
import django
django.setup()
from UserManage.models import User, Logs, Pid
from Assets.models import Assets
@ -88,18 +90,6 @@ def getwinsize():
return struct.unpack('HHHH', x)[0:2]
# def connect_db(user, passwd, db, host='127.0.0.1', port=3306):
# """This function connect db and return db and cursor"""
# db = MySQLdb.connect(host=host,
# port=port,
# user=user,
# passwd=passwd,
# db=db,
# charset='utf8')
# cursor = db.cursor()
# return db, cursor
def run_cmd(cmd):
"""run command and return stdout"""
pipe = subprocess.Popen(cmd,
@ -121,7 +111,7 @@ def connect(host, port, user, password):
log_date_dir = '%s/%s' % (log_dir, time.strftime('%Y%m%d'))
if not os.path.isdir(log_date_dir):
os.mkdir(log_date_dir)
os.chmod(log_date_dir, 0777)
os.mkdir(log_date_dir, 0777)
structtime_start = time.localtime()
datetime_start = time.strftime('%Y%m%d%H%M%S', structtime_start)
logtime_start = time.strftime('%Y/%m/%d %H:%M:%S', structtime_start)
@ -259,6 +249,14 @@ def exec_cmd_servers(username):
cmd = raw_input('\033[1;32mCmd(s): \033[0m')
if cmd in ['q', 'Q']:
break
exec_log_dir = os.path.join(log_dir, 'exec_cmds')
if not os.path.isdir(exec_log_dir):
os.mkdir(exec_log_dir)
os.chmod(exec_log_dir, 0777)
filename = "%s/%s.log" % (exec_log_dir, time.strftime('%Y%m%d'))
f = open(filename, 'a')
f.write("DateTime: %s User: %s Host: %s Cmds: %s\n" %
(time.strftime('%Y/%m/%d %H:%M:%S'), username, hosts, cmd))
for host in hosts:
remote_exec_cmd(host, username, cmd)

View File

@ -34,6 +34,7 @@ ldap_host = cf.get('jumpserver', 'ldap_host')
ldap_base_dn = cf.get('jumpserver', 'ldap_base_dn')
admin_cn = cf.get('jumpserver', 'admin_cn')
admin_pass = cf.get('jumpserver', 'admin_pass')
log_dir = os.path.join(CONF_DIR, 'logs')
def keygen(num):
@ -794,9 +795,6 @@ def addPerm(request):
context_instance=RequestContext(request))
@login_required
def chgPass(request):
"""修改登录密码"""
@ -910,13 +908,24 @@ def upFile(request):
port = asset.port
jm = PyCrypt(key)
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, '%s/%s' % (path, upload_file.name))
msg = u'上传成功,位于 %s主机,位置 %s.' % (host, path)
try:
t = paramiko.Transport((host, port))
t.connect(username=username, password=jm.decrypt(user.ldap_password))
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(filename, '%s/%s' % (path, upload_file.name))
msg = u'上传成功,位于 %s主机,位置 %s.' % (host, path)
uplog_dir = os.path.join(log_dir, 'upload')
if not os.path.isdir(uplog_dir):
os.mkdir(uplog_dir)
filename = os.path.join(uplog_dir, '%s.log' % time.strftime('%Y%m%d'))
f = open(filename, 'a')
f.write('DateTime: %s User: %s Host: %s File: %s\n' %
(time.strftime('%Y/%m/%d %H:%M:%S'), username, host, path))
f.close()
except paramiko.AuthenticationException:
error = u'密码不对 或者 你用的admin用户'
return render_to_response('info.html', {'msg': msg})
return render_to_response('info.html', {'msg': msg, 'error': error})
else:
return render_to_response('info.html', {'error': u"上传失败"})
@ -944,8 +953,15 @@ def downFile(request):
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(path, download_file)
if os.path.isfile(download_file):
downlog_dir = os.path.join(log_dir, 'download')
if not os.path.isdir(downlog_dir):
os.mkdir(downlog_dir)
filename = os.path.join(downlog_dir, '%s.log' % time.strftime('%Y%m%d'))
f = open(filename, 'a')
f.write('DateTime: %s User: %s Host: %s File: %s\n' %
(time.strftime('%Y/%m/%d %H:%M:%S'), username, host, path))
f.close()
wrapper = FileWrapper(open(download_file))
response = HttpResponse(wrapper, mimetype='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(path)
return response

View File

@ -9,6 +9,8 @@ cur_dir = os.path.dirname(__file__)
sys.path.append('%s/webroot/AutoSa/' % cur_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings'
import django
django.setup()
from UserManage.models import Logs, Pid
@ -17,12 +19,12 @@ def log_hanler(logid):
if log:
log = log[0]
filename = log.logfile
ret1 = os.system('cat %s | grep "DateTime" > %s.his' % (filename, filename))
ret2 = os.system('cat %s | grep "\[.*@.*\][\$\#]" >> %s.his' % (filename, filename))
ret3 = os.system('cat %s | grep "EndTime" >> %s.his' % (filename, filename))
if (ret1 + ret2 + ret3) == 0:
print 'Handler % ok.' % filename
if os.path.isfile(filename):
ret1 = os.system('cat %s | grep "DateTime" > %s.his' % (filename, filename))
ret2 = os.system('cat %s | grep "\[.*@.*\][\$\#]" >> %s.his' % (filename, filename))
ret3 = os.system('cat %s | grep "EndTime" >> %s.his' % (filename, filename))
if (ret1 + ret2 + ret3) == 0:
print 'Handler % ok.' % filename
def set_finish(id):

View File

@ -68,6 +68,12 @@
<div class="col-sm-4">
{{ form.group }}
</div>
<div class="col-sm-4">
<a href="/addGroup/">添加属组</a>
</div>
<div class="col-sm-4">
<a href="/showGroup/">删除属组</a>
</div>
</div>
<div class="form-group">
{{ form.is_admin.errors }}

View File

@ -1,3 +0,0 @@
{% extends 'base.html' %}
{% block content %}
{% endblock %}

View File

@ -1,34 +0,0 @@
{% extends "base.html" %}
{% block content %}
<h2 class="text-center">修改主机信息</h2>
<form role="form" action="" method="post">
<div class="form-group has-success">
<label class="control-label" for="inputSuccess">编号</label>
<input type="text" class="form-control" id="inputSuccess" name="s_num" value='{{ post.s_num }}'>
<label class="control-label" for="inputSuccess">IP</label>
<input type="text" class="form-control" id="inputSuccess" name="s_ip" value='{{ post.s_ip }}'>
<label class="control-label" for="inputSuccess">主机名</label>
<input type="text" class="form-control" id="inputSuccess" name="s_hostname" value='{{ post.s_hostname }}'>
<label class="control-label" for="inputSuccess">CPU</label>
<input type="text" class="form-control" id="inputSuccess" name="s_cpu" value='{{ post.s_cpu }}'>
<label class="control-label" for="inputSuccess">内存</label>
<input type="text" class="form-control" id="inputSuccess" name="s_mem" value='{{ post.s_mem }}'>
<label class="control-label" for="inputSuccess">硬盘</label>
<input type="text" class="form-control" id="inputSuccess" name="s_disk" value='{{ post.s_disk }}'>
<label class="control-label" for="inputSuccess">操作系统</label>
<input type="text" class="form-control" id="inputSuccess" name="s_system" value='{{ post.s_system }}'>
<div class="form-group has-success">
<label class="control-label" for="inputSuccess">内核版本</label>
<input type="text" class="form-control" id="inputSuccess" name="s_kernel" value='{{ post.s_kernel }}'>
</div>
<button type="submit" class="btn btn-success col-sm-offset-5">提交更改</button>
</div>
</form>
{% endblock %}

View File

@ -1,58 +0,0 @@
{% extends "base.html" %}
{% block content %}
<div>
<div style="float:left"><h2>主机列表</h2></div>
<div style="float:right;margin-top:16pt"><a href="/assets/add/" target="_black" class="btn btn-info">添加&nbsp;<i class="glyphicon glyphicon-plus-sign"></i></a></div>
</div>
<table class="table table-striped table-hover table-bordered" id="sample_editable_1">
<thead>
<tr>
<th class="text-center">编号</th>
<th class="text-center">IP</th>
<th class="text-center">主机名</th>
<th class="text-center">cpu</th>
<th class="text-center">内存</th>
<th class="text-center">硬盘</th>
<th class="text-center">操作系统</th>
<th class="text-center">详情</th>
<th class="text-center">编辑</th>
<th class="text-center">删除</th>
</tr>
</thead>
<tbody>
{% for post in contacts.object_list %}
<tr class="">
<td class="text-center">{{ post.s_num }}</td>
<td class="text-center">{{ post.s_ip }}</td>
<td class="text-center">{{ post.s_hostname }}</td>
<td class="text-center">{{ post.s_cpu }}</td>
<td class="text-center">{{ post.s_mem }}</td>
<td class="text-center">{{ post.s_disk }}</td>
<td class="text-center">{{ post.s_system }}</td>
<td class="text-center"><a href="/assets/show/{{ post.s_ip }}">详情</a></td>
<td class="text-center"><a href="/assets/edit/{{ post.s_ip }}">编辑</a></td>
<td class="text-center"><a href="/assets/delete/{{ post.s_ip }}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<ul class="pagination">
{% if contacts.has_previous %}
<li><a href="?page={{ contacts.previous_page_number }}">&laquo;</a></li>
{% endif %}
{% for page in p.page_range %}
{% ifequal offset1 page %}
<li class="active"><a href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a></li>
{% else %}
<li><a href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a></li>
{% endifequal %}
{% endfor %}
{% if contacts.has_next %}
<li><a href="?page={{ contacts.next_page_number }}">&raquo;</a></li>
{% endif %}
</ul>
{% endblock %}

View File

@ -41,7 +41,7 @@
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button class="btn btn-primary">修改</button>
<button class="btn btn-success">确认修改</button>
</div>
</div>
</fieldset>

View File

@ -15,6 +15,7 @@
<th>ID</th>
<th>IP</th>
<th>Port</th>
<th>IDC</th>
<th>备注</th>
</tr>
</thead>
@ -25,6 +26,7 @@
<td style="width: 5px">{{ asset.id }}</td>
<td style="width: 90px">{{ asset.ip }}</td>
<td style="width: 90px">{{ asset.port }}</td>
<td style="width: 90px">{{ asset.idc }}</td>
<td style="width: 90px">{{ asset.comment }}</td>
</tr>
{% endfor %}

View File

@ -6,7 +6,7 @@
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><input onclick="selectAll()" type="checkbox" name="select_all" style="select_all" id="select_all"/></th>
<th>id</th>
<th>用户名</th>
<th>主机</th>
<th>监控</th>
@ -19,15 +19,16 @@
<tbody>
{% for log in logs.object_list %}
<tr>
<td><input type="checkbox" name="selected" value="{{ log.id }}"></td>
<!--<td><input type="checkbox" name="selected" value="{{ log.id }}"></td>-->
<td>{{ log.id }}</td>
<td>{{ log.user }}</td>
<td>{{ log.host }}</td>
{% if log.finish %}
<td>监控</td>
<td><a href="/logHistory/?id={{ log.id }}" class="log_command">命令统计</a></td>
<td><a href="/logHistory/?id={{ log.id }}" class="log_command">统计</a></td>
<td>阻断</td>
{% else %}
<td><a href="#">监控</a></td>
<td><span class='monitor' filename="{{ log.logfile }}">监控</span></td>
<td>命令统计</td>
<td><a href="/killSession/?id={{ log.id }}">阻断</a> </td>
{% endif %}

View File

@ -1,43 +0,0 @@
{% extends 'base.html' %}
{% block content %}
<form method="post" action="">
<div style="float: left; width: 400px;">
<div class="row">
<div class="col-lg-12">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" type="button">IP(s)</button>
</span>
<input type="text" class="form-control" name="ip" placeholder="IP or IPS" value={{ old_ip }}>
</div><!-- /input-group -->
<span class="help-block" style="margin-left: 20px;"> 输入一个IP,或者多个IP逗号隔开。</span>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" type="button">CMD</button>
</span>
<input type="text" class="form-control" name="cmd" placeholder="Command or Commands">
</div><!-- /input-group -->
<span class="help-block" style="margin-left: 20px;"> 输入在执行的命令,多个命令分号隔开.</span>
</div>
</div>
</div>
<div style="float: left; margin-top: 74px; margin-left: 40px;">
<button type="submit" class="btn btn-primary">执行</button>
</div>
<div style="clear: both;"></div>
</form>
{% if stdout %}
<div class="alert alert-success">{{ stdout }}</div>
{% endif %}
{% if stderr %}
<div class="alert alert-warning">{{ stderr }}</div>
{% endif %}
{% endblock %}

View File

@ -13,6 +13,7 @@
<th>ID</th>
<th>IP</th>
<th>Port</th>
<th>IDC</th>
<th>备注</th>
</tr>
@ -24,6 +25,7 @@
<td style="width: 5px">{{ asset.id }}</td>
<td style="width: 90px">{{ asset.ip }}</td>
<td style="width: 90px">{{ asset.port }}</td>
<td style="width: 90px">{{ asset.idc }}</td>
<td style="width: 90px">{{ asset.comment }}</td>
</tr>
{% endfor %}

View File

@ -1,33 +0,0 @@
{% extends 'base.html' %}
{% block content %}
<form method="post" action="">
{% if info %}
<p class="alert alert-success">
{{ info }}
</p>
{% endif %}
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><input onclick="selectAll()" type="checkbox" name="select_all" style="select_all" id="select_all"/></th>
<th>ip</th>
<th>用户名</th>
<th>端口</th>
<th>sudo</th>
</tr>
</thead>
<tbody>
{% for server in servers %}
<tr>
<td><input type="checkbox" name="selected" value="{{ server.id }}"></td>
<td>{{ server.ip }}</td>
<td>{{ server.username }}</td>
<td>{{ server.port }}</td>
<td>{{ server.sudo }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button class="btn btn-primary">删除</button>
</form>
{% endblock %}

View File

@ -30,7 +30,6 @@
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>{{ user.name }}</td>
<!--<td>{{ user.group }}</td>-->
<td>
{% for group in user.group.all %}
{{ group.name }}