From 987b1c2c36d69fba70ebc6e5077403d832f2d288 Mon Sep 17 00:00:00 2001 From: kelianchun_miller Date: Wed, 11 May 2016 11:10:48 +0800 Subject: [PATCH 1/3] fix passwd input --- connect.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/connect.py b/connect.py index 8e138c6c5..f11dbf9d9 100755 --- a/connect.py +++ b/connect.py @@ -305,7 +305,6 @@ class SshTty(Tty): old_tty = termios.tcgetattr(sys.stdin) pre_timestamp = time.time() data = '' - input_str = '' input_mode = False try: tty.setraw(sys.stdin.fileno()) @@ -325,8 +324,7 @@ class SshTty(Tty): x = self.channel.recv(10240) if len(x) == 0: break - if self.vim_flag: - self.vim_data += x + index = 0 len_x = len(x) while index < len_x: @@ -347,11 +345,11 @@ class SshTty(Tty): pre_timestamp = now_timestamp log_file_f.flush() - if input_mode and not self.is_output(x): + if self.vim_flag: + self.vim_data += x + elif input_mode: data += x - input_str = '' - except socket.timeout: pass @@ -362,11 +360,7 @@ class SshTty(Tty): pass termlog.recoder = True input_mode = True - input_str += x - if str(x) in ['\r', '\n', '\r\n']: - # 这个是用来处理用户的复制操作 - if input_str != x: - data += input_str + if self.is_output(str(x)): if self.vim_flag: match = self.vim_end_pattern.findall(self.vim_data) if match: @@ -376,11 +370,13 @@ class SshTty(Tty): else: self.vim_end_flag = True else: + # 如果len(str(x)) > 1 说明是复制输入的 + if len(str(x)) > 1: + data = x data = self.deal_command(data)[0:200] if len(data) > 0: TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save() data = '' - input_str = '' self.vim_data = '' input_mode = False From dff50305dea035d0f464f420da09c2b7d959d597 Mon Sep 17 00:00:00 2001 From: kelianchun_miller Date: Wed, 11 May 2016 17:18:40 +0800 Subject: [PATCH 2/3] fix passwd input --- connect.py | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/connect.py b/connect.py index f11dbf9d9..5073d2d47 100755 --- a/connect.py +++ b/connect.py @@ -93,9 +93,7 @@ class Tty(object): self.remote_ip = '' self.login_type = login_type self.vim_flag = False - self.vim_end_flag = False self.vim_end_pattern = re.compile(r'\x1b\[\?1049', re.X) - self.vim_pattern = re.compile(r'\W?vi[m]?\s.* | \W?fg\s.*', re.X) self.vim_data = '' self.stream = None self.screen = None @@ -117,7 +115,8 @@ class Tty(object): return True return False - def command_parser(self, command): + @staticmethod + def command_parser(command): """ 处理命令中如果有ps1或者mysql的特殊情况,极端情况下会有ps1和mysql :param command:要处理的字符传 @@ -157,14 +156,10 @@ class Tty(object): else: command = line_data break - if command != '': - # 判断用户输入的是否是vim 或者fg命令 - if self.vim_pattern.search(command): - self.vim_flag = True - # 虚拟屏幕清空 - self.screen.reset() except Exception: pass + # 虚拟屏幕清空 + self.screen.reset() return command def get_log(self): @@ -345,9 +340,8 @@ class SshTty(Tty): pre_timestamp = now_timestamp log_file_f.flush() - if self.vim_flag: - self.vim_data += x - elif input_mode: + self.vim_data += x + if input_mode: data += x except socket.timeout: @@ -361,21 +355,20 @@ class SshTty(Tty): termlog.recoder = True input_mode = True if self.is_output(str(x)): - if self.vim_flag: - match = self.vim_end_pattern.findall(self.vim_data) - if match: - if self.vim_end_flag or len(match) == 2: - self.vim_flag = False - self.vim_end_flag = False - else: - self.vim_end_flag = True - else: - # 如果len(str(x)) > 1 说明是复制输入的 - if len(str(x)) > 1: - data = x + # 如果len(str(x)) > 1 说明是复制输入的 + if len(str(x)) > 1: + data = x + match = self.vim_end_pattern.findall(self.vim_data) + if match: + if self.vim_flag or len(match) == 2: + self.vim_flag = False + else: + self.vim_flag = True + elif not self.vim_flag: + self.vim_flag = False data = self.deal_command(data)[0:200] - if len(data) > 0: - TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save() + if data is not None: + TtyLog(log=log, datetime=datetime.datetime.now(), cmd=result).save() data = '' self.vim_data = '' input_mode = False From 3ef3b452e29ec5a585a314ab577d7bc418bff5cd Mon Sep 17 00:00:00 2001 From: kelianchun_miller Date: Wed, 11 May 2016 17:21:15 +0800 Subject: [PATCH 3/3] fix passwd input --- connect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect.py b/connect.py index 5073d2d47..f9adccd79 100755 --- a/connect.py +++ b/connect.py @@ -368,7 +368,7 @@ class SshTty(Tty): self.vim_flag = False data = self.deal_command(data)[0:200] if data is not None: - TtyLog(log=log, datetime=datetime.datetime.now(), cmd=result).save() + TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save() data = '' self.vim_data = '' input_mode = False