|
|
@ -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,8 +78,7 @@ class Tty(object):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def deal_command(self, str_r):
|
|
|
|
def deal_command(str_r):
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
处理命令中特殊字符
|
|
|
|
处理命令中特殊字符
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -172,10 +154,9 @@ 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()
|
|
|
|