mirror of https://github.com/jumpserver/jumpserver
global SSH_FLAG to self.ssh_flag
parent
c359d1e264
commit
70db8c69d2
40
connect.py
40
connect.py
|
@ -26,7 +26,6 @@ from jumpserver.settings import LOG_DIR
|
|||
from jperm.ansible_api import Command
|
||||
|
||||
login_user = get_object(User, username=getpass.getuser())
|
||||
VIM_FLAG = False
|
||||
|
||||
try:
|
||||
import termios
|
||||
|
@ -52,23 +51,6 @@ def color_print(msg, color='red', exits=False):
|
|||
sys.exit()
|
||||
|
||||
|
||||
def check_vim_status(command, ssh):
|
||||
global SSH_TTY
|
||||
print command
|
||||
if command == '':
|
||||
return True
|
||||
else:
|
||||
command_str= 'ps -ef |grep "%s" | grep "%s"|grep -v grep |wc -l' % (command,SSH_TTY)
|
||||
print command_str
|
||||
stdin, stdout, stderr = ssh.exec_command(command_str)
|
||||
ps_num = stdout.read()
|
||||
print ps_num
|
||||
if int(ps_num) == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
class Tty(object):
|
||||
"""
|
||||
A virtual tty class
|
||||
|
@ -86,6 +68,7 @@ class Tty(object):
|
|||
self.ssh = None
|
||||
self.connect_info = None
|
||||
self.login_type = 'ssh'
|
||||
self.vim_flag = False
|
||||
|
||||
@staticmethod
|
||||
def is_output(strings):
|
||||
|
@ -95,8 +78,7 @@ class Tty(object):
|
|||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def deal_command(str_r):
|
||||
def deal_command(self, str_r):
|
||||
"""
|
||||
处理命令中特殊字符
|
||||
"""
|
||||
|
@ -172,10 +154,9 @@ class Tty(object):
|
|||
[\x80-\x9f] | (?:\x1b\]0.*) | \[.*@.*\][\$#] | (.*mysql>.*) #匹配 所有控制字符
|
||||
""", re.X)
|
||||
result_command = control_char.sub('', result_command.strip())
|
||||
global VIM_FLAG
|
||||
if not VIM_FLAG:
|
||||
if not self.vim_flag:
|
||||
if result_command.startswith('vi'):
|
||||
VIM_FLAG = True
|
||||
self.vim_flag = True
|
||||
return result_command.decode('utf8', "ignore")
|
||||
else:
|
||||
return ''
|
||||
|
@ -247,11 +228,6 @@ class Tty(object):
|
|||
"""
|
||||
获取需要登陆的主机的信息和映射用户的账号密码
|
||||
"""
|
||||
|
||||
# 1. get ip, port
|
||||
# 2. get 映射用户
|
||||
# 3. get 映射用户的账号,密码或者key
|
||||
# self.connect_info = {'user': '', 'asset': '', 'ip': '', 'port': 0, 'role_name': '', 'role_pass': '', 'role_key': ''}
|
||||
asset_info = get_asset_info(self.asset)
|
||||
self.connect_info = {'user': self.user, 'asset': self.asset, 'ip': asset_info.get('ip'),
|
||||
'port': int(asset_info.get('port')), 'role_name': self.role.name,
|
||||
|
@ -340,8 +316,6 @@ class SshTty(Tty):
|
|||
data = ''
|
||||
chan_str = ''
|
||||
input_mode = False
|
||||
global VIM_FLAG
|
||||
|
||||
try:
|
||||
tty.setraw(sys.stdin.fileno())
|
||||
tty.setcbreak(sys.stdin.fileno())
|
||||
|
@ -358,7 +332,7 @@ class SshTty(Tty):
|
|||
x = self.channel.recv(1024)
|
||||
if len(x) == 0:
|
||||
break
|
||||
if VIM_FLAG:
|
||||
if self.vim_flag:
|
||||
chan_str += x
|
||||
sys.stdout.write(x)
|
||||
sys.stdout.flush()
|
||||
|
@ -380,10 +354,10 @@ class SshTty(Tty):
|
|||
input_mode = True
|
||||
|
||||
if str(x) in ['\r', '\n', '\r\n']:
|
||||
if VIM_FLAG:
|
||||
if self.vim_flag:
|
||||
match = pattern.search(chan_str)
|
||||
if match:
|
||||
VIM_FLAG = False
|
||||
self.vim_flag = False
|
||||
data = self.deal_command(data)
|
||||
if len(data) > 0:
|
||||
TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save()
|
||||
|
|
Loading…
Reference in New Issue