From bd885da17997c25f1171051c51d6d037841ea10f Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 27 Nov 2015 17:53:11 +0800 Subject: [PATCH] =?UTF-8?q?recoard=20=E7=89=B9=E6=AE=8A=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E5=8E=BB=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connect.py | 30 ++++++++++++++---------------- jlog/log_api.py | 9 ++++++++- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/connect.py b/connect.py index 89a62c998..4f79fe45d 100644 --- a/connect.py +++ b/connect.py @@ -53,6 +53,12 @@ def color_print(msg, color='red', exits=False): return msg +def write_log(f, msg): + msg = re.sub(r'[\r\n]', '\r\n', msg) + f.write(msg) + f.flush() + + class Tty(object): """ A virtual tty class @@ -603,19 +609,15 @@ class Nav(object): while True: print "请输入执行的命令, 按q退出" data = 'ansible> ' - log_file_f.write(data) - log_file_f.flush() + write_log(log_file_f, data) now_timestamp = time.time() - log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) - log_time_f.flush() + write_log(log_time_f, '%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) pre_timestamp = now_timestamp command = raw_input("\033[1;32mCmds>:\033[0m ").strip() data = '%s\r\n' % command - log_file_f.write(data) - log_file_f.flush() + write_log(log_file_f, data) now_timestamp = time.time() - log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) - log_time_f.flush() + write_log(log_time_f, '%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) pre_timestamp = now_timestamp TtyLog(log=log, cmd=command, datetime=datetime.datetime.now()).save() if command == 'q': @@ -632,10 +634,8 @@ class Nav(object): output = re.sub(r'[\r\n]', '\r\n', output) data = '%s\r\n%s\r\n' % (header, output) now_timestamp = time.time() - log_file_f.write(data) - log_file_f.flush() - log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) - log_time_f.flush() + write_log(log_file_f, data) + write_log(log_time_f, '%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) pre_timestamp = now_timestamp print else: @@ -645,10 +645,8 @@ class Nav(object): output = re.sub(r'[\r\n]', '\r\n', output) data = '%s\r\n%s\r\n' % (header, output) now_timestamp = time.time() - log_file_f.write(data) - log_file_f.flush() - log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) - log_time_f.flush() + write_log(log_file_f, data) + write_log(log_time_f, '%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(data))) pre_timestamp = now_timestamp print print "=" * 20 diff --git a/jlog/log_api.py b/jlog/log_api.py index 6afbfdb0f..af6e1418d 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -6,6 +6,7 @@ from contextlib import closing from codecs import open as copen from json import dumps from math import ceil +import re from os.path import basename, dirname, exists, join from struct import unpack from subprocess import Popen @@ -19,10 +20,16 @@ from jumpserver.api import BASE_DIR DEFAULT_TEMPLATE = join(BASE_DIR, 'templates', 'jlog', 'static.jinja2') +rz_pat = re.compile(r'\x18B\w+\r\x8a(\x11)?') def escapeString(string): - string = string.encode('unicode_escape').decode('utf-8') + string = rz_pat.sub('', string) + try: + string = string.encode('unicode_escape').decode('utf-8', 'ignore') + except (UnicodeEncodeError, UnicodeDecodeError): + string = string.decode('utf-8', 'ignore') + print repr(string) string = string.replace("'", "\\'") string = '\'' + string + '\'' return string