|
|
@ -26,7 +26,6 @@ from jumpserver.settings import LOG_DIR
|
|
|
|
from jperm.ansible_api import Command
|
|
|
|
from jperm.ansible_api import Command
|
|
|
|
|
|
|
|
|
|
|
|
login_user = get_object(User, username=getpass.getuser())
|
|
|
|
login_user = get_object(User, username=getpass.getuser())
|
|
|
|
VIM_FLAG = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import termios
|
|
|
|
import termios
|
|
|
@ -52,23 +51,6 @@ def color_print(msg, color='red', exits=False):
|
|
|
|
sys.exit()
|
|
|
|
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):
|
|
|
|
class Tty(object):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
A virtual tty class
|
|
|
|
A virtual tty class
|
|
|
@ -86,6 +68,7 @@ class Tty(object):
|
|
|
|
self.ssh = None
|
|
|
|
self.ssh = None
|
|
|
|
self.connect_info = None
|
|
|
|
self.connect_info = None
|
|
|
|
self.login_type = 'ssh'
|
|
|
|
self.login_type = 'ssh'
|
|
|
|
|
|
|
|
self.vim_flag = False
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def is_output(strings):
|
|
|
|
def is_output(strings):
|
|
|
@ -95,25 +78,24 @@ class Tty(object):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def deal_command(self, str_r):
|
|
|
|
def deal_command(str_r):
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
处理命令中特殊字符
|
|
|
|
处理命令中特殊字符
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
str_r = re.sub('\x07','',str_r) #删除响铃
|
|
|
|
str_r = re.sub('\x07', '', str_r) # 删除响铃
|
|
|
|
patch_char = re.compile('\x08\x1b\[C') #删除方向左右一起的按键
|
|
|
|
patch_char = re.compile('\x08\x1b\[C') # 删除方向左右一起的按键
|
|
|
|
while patch_char.search(str_r):
|
|
|
|
while patch_char.search(str_r):
|
|
|
|
str_r = patch_char.sub('', str_r.rstrip())
|
|
|
|
str_r = patch_char.sub('', str_r.rstrip())
|
|
|
|
|
|
|
|
|
|
|
|
result_command = '' #最后的结果
|
|
|
|
result_command = '' # 最后的结果
|
|
|
|
backspace_num = 0 #光标移动的个数
|
|
|
|
backspace_num = 0 # 光标移动的个数
|
|
|
|
reach_backspace_flag = False #没有检测到光标键则为true
|
|
|
|
reach_backspace_flag = False # 没有检测到光标键则为true
|
|
|
|
pattern_str=''
|
|
|
|
pattern_str = ''
|
|
|
|
while str_r:
|
|
|
|
while str_r:
|
|
|
|
tmp = re.match(r'\s*\w+\s*', str_r)
|
|
|
|
tmp = re.match(r'\s*\w+\s*', str_r)
|
|
|
|
if tmp:
|
|
|
|
if tmp:
|
|
|
|
if reach_backspace_flag :
|
|
|
|
if reach_backspace_flag:
|
|
|
|
pattern_str +=str(tmp.group(0))
|
|
|
|
pattern_str += str(tmp.group(0))
|
|
|
|
str_r = str_r[len(str(tmp.group(0))):]
|
|
|
|
str_r = str_r[len(str(tmp.group(0))):]
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -124,7 +106,7 @@ class Tty(object):
|
|
|
|
tmp = re.match(r'\x1b\[K[\x08]*', str_r)
|
|
|
|
tmp = re.match(r'\x1b\[K[\x08]*', str_r)
|
|
|
|
if tmp:
|
|
|
|
if tmp:
|
|
|
|
if backspace_num > 0:
|
|
|
|
if backspace_num > 0:
|
|
|
|
if backspace_num > len(result_command) :
|
|
|
|
if backspace_num > len(result_command):
|
|
|
|
result_command += pattern_str
|
|
|
|
result_command += pattern_str
|
|
|
|
result_command = result_command[0:-backspace_num]
|
|
|
|
result_command = result_command[0:-backspace_num]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -134,8 +116,8 @@ class Tty(object):
|
|
|
|
if del_len > 0:
|
|
|
|
if del_len > 0:
|
|
|
|
result_command = result_command[0:-del_len]
|
|
|
|
result_command = result_command[0:-del_len]
|
|
|
|
reach_backspace_flag = False
|
|
|
|
reach_backspace_flag = False
|
|
|
|
backspace_num =0
|
|
|
|
backspace_num = 0
|
|
|
|
pattern_str=''
|
|
|
|
pattern_str = ''
|
|
|
|
str_r = str_r[len(str(tmp.group(0))):]
|
|
|
|
str_r = str_r[len(str(tmp.group(0))):]
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
@ -153,13 +135,13 @@ class Tty(object):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if reach_backspace_flag :
|
|
|
|
if reach_backspace_flag:
|
|
|
|
pattern_str +=str_r[0]
|
|
|
|
pattern_str += str_r[0]
|
|
|
|
else :
|
|
|
|
else:
|
|
|
|
result_command += str_r[0]
|
|
|
|
result_command += str_r[0]
|
|
|
|
str_r = str_r[1:]
|
|
|
|
str_r = str_r[1:]
|
|
|
|
|
|
|
|
|
|
|
|
if backspace_num > 0 :
|
|
|
|
if backspace_num > 0:
|
|
|
|
result_command = result_command[0:-backspace_num] + pattern_str
|
|
|
|
result_command = result_command[0:-backspace_num] + pattern_str
|
|
|
|
|
|
|
|
|
|
|
|
control_char = re.compile(r"""
|
|
|
|
control_char = re.compile(r"""
|
|
|
@ -172,11 +154,10 @@ class Tty(object):
|
|
|
|
[\x80-\x9f] | (?:\x1b\]0.*) | \[.*@.*\][\$#] | (.*mysql>.*) #匹配 所有控制字符
|
|
|
|
[\x80-\x9f] | (?:\x1b\]0.*) | \[.*@.*\][\$#] | (.*mysql>.*) #匹配 所有控制字符
|
|
|
|
""", re.X)
|
|
|
|
""", re.X)
|
|
|
|
result_command = control_char.sub('', result_command.strip())
|
|
|
|
result_command = control_char.sub('', result_command.strip())
|
|
|
|
global VIM_FLAG
|
|
|
|
if not self.vim_flag:
|
|
|
|
if not VIM_FLAG:
|
|
|
|
|
|
|
|
if result_command.startswith('vi'):
|
|
|
|
if result_command.startswith('vi'):
|
|
|
|
VIM_FLAG = True
|
|
|
|
self.vim_flag = True
|
|
|
|
return result_command.decode('utf8',"ignore")
|
|
|
|
return result_command.decode('utf8', "ignore")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
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)
|
|
|
|
asset_info = get_asset_info(self.asset)
|
|
|
|
self.connect_info = {'user': self.user, 'asset': self.asset, 'ip': asset_info.get('ip'),
|
|
|
|
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,
|
|
|
|
'port': int(asset_info.get('port')), 'role_name': self.role.name,
|
|
|
@ -340,8 +316,6 @@ class SshTty(Tty):
|
|
|
|
data = ''
|
|
|
|
data = ''
|
|
|
|
chan_str = ''
|
|
|
|
chan_str = ''
|
|
|
|
input_mode = False
|
|
|
|
input_mode = False
|
|
|
|
global VIM_FLAG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
tty.setraw(sys.stdin.fileno())
|
|
|
|
tty.setraw(sys.stdin.fileno())
|
|
|
|
tty.setcbreak(sys.stdin.fileno())
|
|
|
|
tty.setcbreak(sys.stdin.fileno())
|
|
|
@ -358,7 +332,7 @@ class SshTty(Tty):
|
|
|
|
x = self.channel.recv(1024)
|
|
|
|
x = self.channel.recv(1024)
|
|
|
|
if len(x) == 0:
|
|
|
|
if len(x) == 0:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
if VIM_FLAG:
|
|
|
|
if self.vim_flag:
|
|
|
|
chan_str += x
|
|
|
|
chan_str += x
|
|
|
|
sys.stdout.write(x)
|
|
|
|
sys.stdout.write(x)
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stdout.flush()
|
|
|
@ -380,10 +354,10 @@ class SshTty(Tty):
|
|
|
|
input_mode = True
|
|
|
|
input_mode = True
|
|
|
|
|
|
|
|
|
|
|
|
if str(x) in ['\r', '\n', '\r\n']:
|
|
|
|
if str(x) in ['\r', '\n', '\r\n']:
|
|
|
|
if VIM_FLAG:
|
|
|
|
if self.vim_flag:
|
|
|
|
match = pattern.search(chan_str)
|
|
|
|
match = pattern.search(chan_str)
|
|
|
|
if match:
|
|
|
|
if match:
|
|
|
|
VIM_FLAG = False
|
|
|
|
self.vim_flag = False
|
|
|
|
data = self.deal_command(data)
|
|
|
|
data = self.deal_command(data)
|
|
|
|
if len(data) > 0:
|
|
|
|
if len(data) > 0:
|
|
|
|
TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save()
|
|
|
|
TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save()
|
|
|
|