web terminal kill方法

pull/26/head
ibuler 2015-11-07 15:27:49 +08:00
parent 24730ebd0f
commit 98c4d9bdba
5 changed files with 46 additions and 14 deletions

View File

@ -269,12 +269,7 @@ class Tty(object):
today_connect_log_dir = os.path.join(tty_log_dir, date_start) today_connect_log_dir = os.path.join(tty_log_dir, date_start)
log_file_path = os.path.join(today_connect_log_dir, '%s_%s_%s' % (self.username, self.asset_name, time_start)) log_file_path = os.path.join(today_connect_log_dir, '%s_%s_%s' % (self.username, self.asset_name, time_start))
if self.login_type == 'ssh':
pid = os.getpid()
remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n')
else:
pid = 0
remote_ip = 'Web'
try: try:
is_dir(today_connect_log_dir, mode=0777) is_dir(today_connect_log_dir, mode=0777)
@ -287,8 +282,19 @@ class Tty(object):
except IOError: except IOError:
raise ServerError('Create logfile failed, Please modify %s permission.' % today_connect_log_dir) raise ServerError('Create logfile failed, Please modify %s permission.' % today_connect_log_dir)
log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip, if self.login_type == 'ssh':
log_path=log_file_path, start_time=datetime.datetime.now(), pid=pid) pid = os.getpid()
remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n')
log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip,
log_path=log_file_path, start_time=datetime.datetime.now(), pid=pid)
else:
remote_ip = 'Web'
log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip,
log_path=log_file_path, start_time=datetime.datetime.now(), pid=0)
log.save()
log.pid = log.id
log.save()
log_file_f.write('Start at %s\n' % datetime.datetime.now()) log_file_f.write('Start at %s\n' % datetime.datetime.now())
log.save() log.save()
return log_file_f, log_time_f, log return log_file_f, log_time_f, log

View File

@ -48,7 +48,8 @@ def log_list(request, offset):
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
web_monitor_uri = '%s/monitor' % web_socket_host web_monitor_uri = 'ws://%s/monitor' % web_socket_host
web_kill_uri = 'http://%s/kill' % web_socket_host
return render_to_response('jlog/log_%s.html' % offset, locals(), context_instance=RequestContext(request)) return render_to_response('jlog/log_%s.html' % offset, locals(), context_instance=RequestContext(request))
@ -103,6 +104,6 @@ def log_record(request):
def web_terminal(request): def web_terminal(request):
web_terminal_uri = '%s/terminal' % web_socket_host web_terminal_uri = 'ws://%s/terminal' % web_socket_host
return render_to_response('jlog/web_terminal.html', locals()) return render_to_response('jlog/web_terminal.html', locals())

View File

@ -23,7 +23,7 @@ root_pw = secret234
[websocket] [websocket]
web_socket_host = ws://192.168.244.129:3000 web_socket_host = 192.168.244.129:3000
[mail] [mail]

View File

@ -7,6 +7,7 @@ import os
import sys import sys
import os.path import os.path
import threading import threading
import uuid
import tornado.ioloop import tornado.ioloop
import tornado.options import tornado.options
@ -96,6 +97,7 @@ class Application(tornado.web.Application):
handlers = [ handlers = [
(r'/monitor', MonitorHandler), (r'/monitor', MonitorHandler),
(r'/terminal', WebTerminalHandler), (r'/terminal', WebTerminalHandler),
(r'/kill', WebTerminalKillHandler),
] ]
setting = { setting = {
@ -165,8 +167,20 @@ class WebTty(Tty):
self.input_mode = False self.input_mode = False
class WebTerminalKillHandler(tornado.web.RequestHandler):
def get(self):
ws_id = self.get_argument('id')
for ws in WebTerminalHandler.clients:
print ws.id
if ws.id == int(ws_id):
print "killed"
ws.close()
print len(WebTerminalHandler.clients)
class WebTerminalHandler(tornado.websocket.WebSocketHandler): class WebTerminalHandler(tornado.websocket.WebSocketHandler):
tasks = [] tasks = []
clients = []
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.term = None self.term = None
@ -174,6 +188,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
self.log_file_f = None self.log_file_f = None
self.log_time_f = None self.log_time_f = None
self.log = None self.log = None
self.id = 0
super(WebTerminalHandler, self).__init__(*args, **kwargs) super(WebTerminalHandler, self).__init__(*args, **kwargs)
def check_origin(self, origin): def check_origin(self, origin):
@ -184,6 +199,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
self.term.get_connection() self.term.get_connection()
self.channel = self.term.ssh.invoke_shell(term='xterm') self.channel = self.term.ssh.invoke_shell(term='xterm')
WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound)) WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound))
WebTerminalHandler.clients.append(self)
for t in WebTerminalHandler.tasks: for t in WebTerminalHandler.tasks:
if t.is_alive(): if t.is_alive():
@ -205,6 +221,8 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
def on_close(self): def on_close(self):
print 'On_close' print 'On_close'
if self in WebTerminalHandler.clients:
WebTerminalHandler.clients.remove(self)
self.log_file_f.write('End time is %s' % datetime.datetime.now()) self.log_file_f.write('End time is %s' % datetime.datetime.now())
self.log.is_finished = True self.log.is_finished = True
self.log.end_time = datetime.datetime.now() self.log.end_time = datetime.datetime.now()
@ -213,6 +231,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
def forward_outbound(self): def forward_outbound(self):
self.log_file_f, self.log_time_f, self.log = self.term.get_log_file() self.log_file_f, self.log_time_f, self.log = self.term.get_log_file()
self.id = self.log.id
try: try:
data = '' data = ''
pre_timestamp = time.time() pre_timestamp = time.time()

View File

@ -97,7 +97,7 @@
{% ifnotequal session_role_id 0 %} {% ifnotequal session_role_id 0 %}
<td class="text-center"><a href="/jlog/history/?id={{ post.id }}" class="log_command"> 命令统计 </a></td> <td class="text-center"><a href="/jlog/history/?id={{ post.id }}" class="log_command"> 命令统计 </a></td>
<td class="text-center"><a class="monitor" file_path="{{ post.log_path }}"> 监控 </a></td> <td class="text-center"><a class="monitor" file_path="{{ post.log_path }}"> 监控 </a></td>
<td class="text-center"><input type="button" id="cut" class="btn btn-danger btn-xs" name="cut" value="阻断" onclick='cut("{{ post.pid }}")' /></td> <td class="text-center"><input type="button" id="cut" class="btn btn-danger btn-xs" name="cut" value="阻断" onclick='cut("{{ post.pid }}", "{{ post.remote_ip }}")' /></td>
{% endifnotequal %} {% endifnotequal %}
<td class="text-center" id="start_time"> {{ post.start_time|date:"Y-m-d H:i:s" }} </td> <td class="text-center" id="start_time"> {{ post.start_time|date:"Y-m-d H:i:s" }} </td>
</tr> </tr>
@ -202,8 +202,14 @@
{# }#} {# }#}
function cut(num){ function cut(num, host){
var g_url = "/jlog/log_kill/?id="+num; console.log(host);
if (host=='Web'){
var g_url = '{{ web_kill_uri }}' + '?id=' + num;
} else {
g_url = "/jlog/log_kill/?id=" + num;
}
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: g_url, url: g_url,