mirror of https://github.com/jumpserver/jumpserver
web terminal kill方法
parent
24730ebd0f
commit
98c4d9bdba
22
connect.py
22
connect.py
|
@ -269,12 +269,7 @@ class Tty(object):
|
|||
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))
|
||||
|
||||
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:
|
||||
is_dir(today_connect_log_dir, mode=0777)
|
||||
|
@ -287,8 +282,19 @@ class Tty(object):
|
|||
except IOError:
|
||||
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,
|
||||
log_path=log_file_path, start_time=datetime.datetime.now(), pid=pid)
|
||||
if self.login_type == 'ssh':
|
||||
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.save()
|
||||
return log_file_f, log_time_f, log
|
||||
|
|
|
@ -48,7 +48,8 @@ def log_list(request, offset):
|
|||
|
||||
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))
|
||||
|
||||
|
||||
|
@ -103,6 +104,6 @@ def log_record(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())
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ root_pw = secret234
|
|||
|
||||
|
||||
[websocket]
|
||||
web_socket_host = ws://192.168.244.129:3000
|
||||
web_socket_host = 192.168.244.129:3000
|
||||
|
||||
|
||||
[mail]
|
||||
|
|
|
@ -7,6 +7,7 @@ import os
|
|||
import sys
|
||||
import os.path
|
||||
import threading
|
||||
import uuid
|
||||
|
||||
import tornado.ioloop
|
||||
import tornado.options
|
||||
|
@ -96,6 +97,7 @@ class Application(tornado.web.Application):
|
|||
handlers = [
|
||||
(r'/monitor', MonitorHandler),
|
||||
(r'/terminal', WebTerminalHandler),
|
||||
(r'/kill', WebTerminalKillHandler),
|
||||
]
|
||||
|
||||
setting = {
|
||||
|
@ -165,8 +167,20 @@ class WebTty(Tty):
|
|||
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):
|
||||
tasks = []
|
||||
clients = []
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.term = None
|
||||
|
@ -174,6 +188,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
|
|||
self.log_file_f = None
|
||||
self.log_time_f = None
|
||||
self.log = None
|
||||
self.id = 0
|
||||
super(WebTerminalHandler, self).__init__(*args, **kwargs)
|
||||
|
||||
def check_origin(self, origin):
|
||||
|
@ -184,6 +199,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
|
|||
self.term.get_connection()
|
||||
self.channel = self.term.ssh.invoke_shell(term='xterm')
|
||||
WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound))
|
||||
WebTerminalHandler.clients.append(self)
|
||||
|
||||
for t in WebTerminalHandler.tasks:
|
||||
if t.is_alive():
|
||||
|
@ -205,6 +221,8 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
|
|||
|
||||
def on_close(self):
|
||||
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.is_finished = True
|
||||
self.log.end_time = datetime.datetime.now()
|
||||
|
@ -213,6 +231,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
|
|||
|
||||
def forward_outbound(self):
|
||||
self.log_file_f, self.log_time_f, self.log = self.term.get_log_file()
|
||||
self.id = self.log.id
|
||||
try:
|
||||
data = ''
|
||||
pre_timestamp = time.time()
|
||||
|
|
|
@ -97,7 +97,7 @@
|
|||
{% 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 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 %}
|
||||
<td class="text-center" id="start_time"> {{ post.start_time|date:"Y-m-d H:i:s" }} </td>
|
||||
</tr>
|
||||
|
@ -202,8 +202,14 @@
|
|||
{# }#}
|
||||
|
||||
|
||||
function cut(num){
|
||||
var g_url = "/jlog/log_kill/?id="+num;
|
||||
function cut(num, host){
|
||||
console.log(host);
|
||||
if (host=='Web'){
|
||||
var g_url = '{{ web_kill_uri }}' + '?id=' + num;
|
||||
} else {
|
||||
g_url = "/jlog/log_kill/?id=" + num;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
url: g_url,
|
||||
|
|
Loading…
Reference in New Issue