From 598d6cffa47622e6492081399857903bc3706853 Mon Sep 17 00:00:00 2001 From: kelianchun_miller Date: Fri, 18 Mar 2016 20:43:25 +0800 Subject: [PATCH 1/3] pull issue #120 --- connect.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/connect.py b/connect.py index 974868f8f..843e776c6 100755 --- a/connect.py +++ b/connect.py @@ -95,8 +95,18 @@ class Tty(object): self.ps1_pattern = re.compile('\[.*@.*\][\$#]\s') self.vim_pattern = re.compile(r'\Wvi[m]+\s.* | \Wfg\s.*', re.X) self.vim_data = '' - self.stream = pyte.ByteStream() + self.stream = None self.screen = None + self.__init_screen_stream() + + def __init_screen_stream(self): + """ + 初始化虚拟屏幕和字符流 + :return: + """ + self.stream = pyte.ByteStream() + self.screen = pyte.Screen(80, 24) + self.stream.attach(self.screen) @staticmethod def is_output(strings): @@ -125,12 +135,14 @@ class Tty(object): result = match[-1].strip() return result - def deal_command(self): + def deal_command(self, data): """ 处理截获的命令 + :param data: 要处理的命令 :return:返回最后的处理结果 """ command = '' + self.stream.feed(data) # 从虚拟屏幕中获取处理后的数据 for line in reversed(self.screen.buffer): line_data = "".join(map(operator.attrgetter("data"), line)).strip() @@ -348,16 +360,15 @@ class SshTty(Tty): # 这个是用来处理用户的复制操作 if input_str != x: data += input_str - self.stream.feed(data) if self.vim_flag: match = self.ps1_pattern.search(self.vim_data) if match: self.vim_flag = False - data = self.deal_command()[0:200] + data = self.deal_command(data)[0:200] if len(data) > 0: TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save() else: - data = self.deal_command()[0:200] + data = self.deal_command(data)[0:200] if len(data) > 0: TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save() data = '' @@ -395,8 +406,6 @@ class SshTty(Tty): win_size = self.get_win_size() #self.channel = channel = ssh.invoke_shell(height=win_size[0], width=win_size[1], term='xterm') self.channel = channel = transport.open_session() - self.screen = pyte.Screen(win_size[1], win_size[0]) - self.stream.attach(self.screen) channel.get_pty(term='xterm', height=win_size[0], width=win_size[1]) channel.invoke_shell() try: From 7dcf050dc4c59bf0b139af851bd1b9d0ad716e45 Mon Sep 17 00:00:00 2001 From: kelianchun_miller Date: Fri, 18 Mar 2016 22:06:40 +0800 Subject: [PATCH 2/3] pull issue #120 --- connect.py | 1 - 1 file changed, 1 deletion(-) diff --git a/connect.py b/connect.py index 843e776c6..ad25b1511 100755 --- a/connect.py +++ b/connect.py @@ -102,7 +102,6 @@ class Tty(object): def __init_screen_stream(self): """ 初始化虚拟屏幕和字符流 - :return: """ self.stream = pyte.ByteStream() self.screen = pyte.Screen(80, 24) From b2ccbc3f9ec7bad3f1fb4545eddc1e26f2aba158 Mon Sep 17 00:00:00 2001 From: kelianchun_miller Date: Fri, 18 Mar 2016 23:36:31 +0800 Subject: [PATCH 3/3] pull issue #120 --- connect.py | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/connect.py b/connect.py index ad25b1511..269eea07c 100755 --- a/connect.py +++ b/connect.py @@ -92,8 +92,8 @@ class Tty(object): self.remote_ip = '' self.login_type = login_type self.vim_flag = False - self.ps1_pattern = re.compile('\[.*@.*\][\$#]\s') - self.vim_pattern = re.compile(r'\Wvi[m]+\s.* | \Wfg\s.*', re.X) + self.ps1_pattern = re.compile('\[?.*@.*\]?[\$#]\s') + self.vim_pattern = re.compile(r'\W?vi[m]?\s.* | \W?fg\s.*', re.X) self.vim_data = '' self.stream = None self.screen = None @@ -141,25 +141,28 @@ class Tty(object): :return:返回最后的处理结果 """ command = '' - self.stream.feed(data) - # 从虚拟屏幕中获取处理后的数据 - for line in reversed(self.screen.buffer): - line_data = "".join(map(operator.attrgetter("data"), line)).strip() - if len(line_data) > 0: - parser_result = self.command_parser(line_data) - if parser_result is not None: - # 2个条件写一起会有错误的数据 - if len(parser_result) > 0: - command = parser_result - else: - command = line_data - break - if command != '': - # 判断用户输入的是否是vim 或者fg命令 - if self.vim_pattern.search(command): - self.vim_flag = True - # 虚拟屏幕清空 - self.screen.reset() + try: + self.stream.feed(data) + # 从虚拟屏幕中获取处理后的数据 + for line in reversed(self.screen.buffer): + line_data = "".join(map(operator.attrgetter("data"), line)).strip() + if len(line_data) > 0: + parser_result = self.command_parser(line_data) + if parser_result is not None: + # 2个条件写一起会有错误的数据 + if len(parser_result) > 0: + command = parser_result + 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 return command def get_log(self): @@ -403,7 +406,7 @@ class SshTty(Tty): # 获取连接的隧道并设置窗口大小 Make a channel and set windows size global channel win_size = self.get_win_size() - #self.channel = channel = ssh.invoke_shell(height=win_size[0], width=win_size[1], term='xterm') + # self.channel = channel = ssh.invoke_shell(height=win_size[0], width=win_size[1], term='xterm') self.channel = channel = transport.open_session() channel.get_pty(term='xterm', height=win_size[0], width=win_size[1]) channel.invoke_shell()