jumpserver/connect.py

598 lines
22 KiB
Python
Raw Normal View History

2015-04-20 02:12:11 +00:00
# coding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import re
import time
2015-11-04 09:04:25 +00:00
import datetime
2015-04-20 02:12:11 +00:00
import textwrap
import getpass
import readline
2015-06-08 15:46:40 +00:00
import django
2015-11-04 09:04:25 +00:00
import paramiko
import struct, fcntl, signal, socket, select
2015-04-20 02:12:11 +00:00
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
2015-06-08 15:46:40 +00:00
if django.get_version() != '1.6':
django.setup()
2015-11-18 07:15:08 +00:00
from jumpserver.api import ServerError, User, Asset, AssetGroup, get_object, mkdir
from jumpserver.api import logger, Log, TtyLog
from jumpserver.settings import LOG_DIR
login_user = get_object(User, username=getpass.getuser())
2015-11-16 15:33:44 +00:00
VIM_FLAG = False
2015-04-24 10:19:43 +00:00
2015-11-04 09:04:25 +00:00
try:
import termios
import tty
except ImportError:
print '\033[1;31m仅支持类Unix系统 Only unix like supported.\033[0m'
time.sleep(3)
sys.exit()
2015-04-20 02:12:11 +00:00
2015-06-15 11:20:05 +00:00
def color_print(msg, color='red', exits=False):
2015-06-08 15:46:40 +00:00
"""
Print colorful string.
2015-08-26 15:31:32 +00:00
颜色打印字符或者退出
2015-06-08 15:46:40 +00:00
"""
2015-04-20 02:12:11 +00:00
color_msg = {'blue': '\033[1;36m%s\033[0m',
'green': '\033[1;32m%s\033[0m',
'red': '\033[1;31m%s\033[0m'}
print color_msg.get(color, 'blue') % msg
2015-06-15 11:20:05 +00:00
if exits:
time.sleep(2)
sys.exit()
2015-11-04 09:04:25 +00:00
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
2015-11-07 05:38:50 +00:00
class Tty(object):
2015-11-04 09:04:25 +00:00
"""
A virtual tty class
2015-11-07 05:38:50 +00:00
一个虚拟终端类实现连接ssh和记录日志基类
2015-11-04 09:04:25 +00:00
"""
2015-11-07 05:38:50 +00:00
def __init__(self, username, asset_name):
2015-11-04 09:04:25 +00:00
self.username = username
2015-11-07 05:38:50 +00:00
self.asset_name = asset_name
self.ip = None
self.port = 22
self.channel = None
#self.asset = get_object(Asset, name=asset_name)
#self.user = get_object(User, username=username)
2015-11-07 05:38:50 +00:00
self.role = None
self.ssh = None
self.connect_info = None
self.login_type = 'ssh'
2015-11-04 09:04:25 +00:00
@staticmethod
2015-11-07 05:38:50 +00:00
def is_output(strings):
newline_char = ['\n', '\r', '\r\n']
for char in newline_char:
if char in strings:
return True
return False
2015-11-04 09:04:25 +00:00
2015-11-16 15:33:44 +00:00
@staticmethod
def deal_command(str_r, ssh):
"""
处理命令中特殊字符
"""
str_r = re.sub('\x07','',str_r) #删除响铃
patch_char = re.compile('\x08\x1b\[C') #删除方向左右一起的按键
while patch_char.search(str_r):
str_r = patch_char.sub('', str_r.rstrip())
result_command = '' #最后的结果
backspace_num = 0 #光标移动的个数
backspace_list = []
reach_backspace_flag = False #没有检测到光标键则为true
reach_backspace_second_flag = False
pattern_list = []
pattern_str=''
while str_r:
tmp = re.match(r'\s*\w+\s*', str_r) #获取字符串,其它特殊字符匹配暂时还不知道。。
if tmp:
if reach_backspace_flag :
if not reach_backspace_second_flag:
pattern_str +=str(tmp.group(0))
else:
pattern_list.append(pattern_str)
pattern_str=str(tmp.group(0))
reach_backspace_second_flag=False
str_r = str_r[len(str(tmp.group(0))):]
continue
else:
result_command += str(tmp.group(0))
str_r = str_r[len(str(tmp.group(0))):]
continue
tmp = re.match(r'\x1b\[K[\x08]*', str_r) #遇到删除确认符,确定删除数据
if tmp:
for x in backspace_list:
backspace_num += int(x)
if backspace_num > 0:
if backspace_num > len(result_command) :
result_command += ''.join(pattern_list)
result_command += pattern_str
result_command = result_command[0:-backspace_num]
else:
result_command = result_command[0:-backspace_num]
result_command += ''.join(pattern_list)
result_command += pattern_str
del_len = len(str(tmp.group(0)))-3
if del_len > 0:
result_command = result_command[0:-del_len]
reach_backspace_flag = False
reach_backspace_second_flag =False
backspace_num =0
del pattern_list[:]
del backspace_list[:]
pattern_str=''
str_r = str_r[len(str(tmp.group(0))):]
continue
tmp = re.match(r'\x08+', str_r) #将遇到的退格数字存放到队列中
if tmp:
if reach_backspace_flag:
reach_backspace_second_flag = True
else:
reach_backspace_flag = True
str_r = str_r[len(str(tmp.group(0))):]
if len(str_r) != 0: #如果退格键在最后,则放弃
backspace_list.append(len(str(tmp.group(0))))
continue
if reach_backspace_flag :
if not reach_backspace_second_flag:
pattern_str +=str_r[0]
else:
pattern_list.append(pattern_str)
pattern_str=str_r[0]
reach_backspace_second_flag=False
else :
result_command += str_r[0]
str_r = str_r[1:]
if pattern_str !='':
pattern_list.append(pattern_str)
#退格队列中还有腿哥键,则进行删除操作
if len(backspace_list) > 0 :
for backspace in backspace_list:
if int(backspace) >= len(result_command):
result_command = pattern_list[0]
else:
result_command = result_command[:-int(backspace)]
result_command += pattern_list[0]
pattern_list = pattern_list[1:]
control_char = re.compile(r"""
\x1b[ #%()*+\-.\/]. |
\r | #匹配 回车符(CR)
(?:\x1b\[|\x9b) [ -?]* [@-~] | #匹配 控制顺序描述符(CSI)... Cmd
(?:\x1b\]|\x9d) .*? (?:\x1b\\|[\a\x9c]) | \x07 | #匹配 操作系统指令(OSC)...终止符或振铃符(ST|BEL)
(?:\x1b[P^_]|[\x90\x9e\x9f]) .*? (?:\x1b\\|\x9c) | #匹配 设备控制串或私讯或应用程序命令(DCS|PM|APC)...终止符(ST)
\x1b. #匹配 转义过后的字符
[\x80-\x9f] | (?:\x1b\]0.*) | \[.*@.*\][\$#] | (.*mysql>.*) #匹配 所有控制字符
""", re.X)
result_command = control_char.sub('', result_command.strip())
global VIM_FLAG
global VIM_COMMAND
if not VIM_FLAG:
if result_command.startswith('vi'):
VIM_FLAG = True
VIM_COMMAND = result_command
return result_command.decode('utf8',"ignore")
else:
if check_vim_status(VIM_COMMAND, ssh):
VIM_FLAG = False
VIM_COMMAND=''
if result_command.endswith(':wq') or result_command.endswith(':wq!') or result_command.endswith(':q!'):
return ''
return result_command.decode('utf8',"ignore")
else:
return ''
2015-11-07 05:38:50 +00:00
@staticmethod
def remove_control_char(str_r):
2015-11-04 09:04:25 +00:00
"""
2015-11-07 05:38:50 +00:00
处理日志特殊字符
2015-11-04 09:04:25 +00:00
"""
2015-11-07 05:38:50 +00:00
control_char = re.compile(r"""
\x1b[ #%()*+\-.\/]. |
\r | #匹配 回车符(CR)
(?:\x1b\[|\x9b) [ -?]* [@-~] | #匹配 控制顺序描述符(CSI)... Cmd
(?:\x1b\]|\x9d) .*? (?:\x1b\\|[\a\x9c]) | \x07 | #匹配 操作系统指令(OSC)...终止符或振铃符(ST|BEL)
(?:\x1b[P^_]|[\x90\x9e\x9f]) .*? (?:\x1b\\|\x9c) | #匹配 设备控制串或私讯或应用程序命令(DCS|PM|APC)...终止符(ST)
\x1b. #匹配 转义过后的字符
[\x80-\x9f] #匹配 所有控制字符
""", re.X)
backspace = re.compile(r"[^\b][\b]")
line_filtered = control_char.sub('', str_r.rstrip())
while backspace.search(line_filtered):
line_filtered = backspace.sub('', line_filtered)
return line_filtered
def get_log(self):
2015-11-04 09:04:25 +00:00
"""
Logging user command and output.
记录用户的日志
"""
tty_log_dir = os.path.join(LOG_DIR, 'tty')
date_today = datetime.datetime.now()
date_start = date_today.strftime('%Y%m%d')
time_start = date_today.strftime('%H%M%S')
2015-11-04 09:04:25 +00:00
today_connect_log_dir = os.path.join(tty_log_dir, date_start)
2015-11-07 05:38:50 +00:00
log_file_path = os.path.join(today_connect_log_dir, '%s_%s_%s' % (self.username, self.asset_name, time_start))
2015-11-04 09:04:25 +00:00
try:
2015-11-18 07:15:08 +00:00
mkdir(os.path.dirname(today_connect_log_dir), mode=0777)
mkdir(today_connect_log_dir, mode=0777)
2015-11-04 09:04:25 +00:00
except OSError:
logger.debug('创建目录 %s 失败,请修改%s目录权限' % (today_connect_log_dir, tty_log_dir))
2015-11-04 09:04:25 +00:00
raise ServerError('Create %s failed, Please modify %s permission.' % (today_connect_log_dir, tty_log_dir))
try:
log_file_f = open(log_file_path + '.log', 'a')
log_time_f = open(log_file_path + '.time', 'a')
except IOError:
logger.debug('创建tty日志文件失败, 请修改目录%s权限' % today_connect_log_dir)
2015-11-04 09:04:25 +00:00
raise ServerError('Create logfile failed, Please modify %s permission.' % today_connect_log_dir)
if self.login_type == 'ssh': # 如果是ssh连接过来记录connect.py的pidweb terminal记录为日志的id
2015-11-07 07:27:49 +00:00
pid = os.getpid()
remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n') # 获取远端IP
2015-11-07 07:27:49 +00:00
log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip,
log_path=log_file_path, start_time=date_today, pid=pid)
2015-11-07 07:27:49 +00:00
else:
remote_ip = 'Web'
log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip,
log_path=log_file_path, start_time=date_today, pid=0)
2015-11-07 07:27:49 +00:00
log.save()
log.pid = log.id
2015-11-04 09:04:25 +00:00
log.save()
log_file_f.write('Start at %s\n' % datetime.datetime.now())
2015-11-07 05:38:50 +00:00
return log_file_f, log_time_f, log
def get_connect_info(self):
"""
获取需要登陆的主机的信息和映射用户的账号密码
"""
# 1. get ip, port
# 2. get 映射用户
# 3. get 映射用户的账号密码或者key
# self.connect_info = {'user': '', 'asset': '', 'ip': '', 'port': 0, 'role_name': '', 'role_pass': '', 'role_key': ''}
2015-11-18 07:15:08 +00:00
self.connect_info = {'user': 'a', 'asset': 'b', 'ip': '127.0.0.1', 'port': 22, 'role_name': 'root', 'role_pass': 'redhat', 'role_key': ''}
2015-11-07 05:38:50 +00:00
return self.connect_info
2015-11-04 09:04:25 +00:00
2015-11-07 05:38:50 +00:00
def get_connection(self):
"""
获取连接成功后的ssh
"""
connect_info = self.get_connect_info()
# 发起ssh连接请求 Make a ssh connection
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
if connect_info.get('role_pass'):
ssh.connect(connect_info.get('ip'),
port=connect_info.get('port'),
username=connect_info.get('role_name'),
password=connect_info.get('role_pass'),
look_for_keys=False)
else:
ssh.connect(connect_info.get('ip'),
port=connect_info.get('port'),
username=connect_info.get('role_name'),
key_filename=connect_info.get('role_key'),
look_for_keys=False)
except paramiko.ssh_exception.AuthenticationException, paramiko.ssh_exception.SSHException:
raise ServerError('认证失败 Authentication Error.')
except socket.error:
raise ServerError('端口可能不对 Connect SSH Socket Port Error, Please Correct it.')
else:
self.ssh = ssh
return ssh
class SshTty(Tty):
"""
A virtual tty class
一个虚拟终端类实现连接ssh和记录日志
"""
@staticmethod
def get_win_size():
"""
This function use to get the size of the windows!
获得terminal窗口大小
"""
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912L
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
def set_win_size(self, sig, data):
"""
This function use to set the window size of the terminal!
设置terminal窗口大小
"""
try:
win_size = self.get_win_size()
self.channel.resize_pty(height=win_size[0], width=win_size[1])
except Exception:
pass
def posix_shell(self):
2015-11-04 09:04:25 +00:00
"""
Use paramiko channel connect server interactive.
使用paramiko模块的channel连接后端进入交互式
"""
log_file_f, log_time_f, log = self.get_log()
2015-11-04 09:04:25 +00:00
old_tty = termios.tcgetattr(sys.stdin)
pre_timestamp = time.time()
data = ''
2015-11-04 09:04:25 +00:00
input_mode = False
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
2015-11-07 05:38:50 +00:00
self.channel.settimeout(0.0)
2015-11-04 09:04:25 +00:00
while True:
try:
2015-11-07 05:38:50 +00:00
r, w, e = select.select([self.channel, sys.stdin], [], [])
2015-11-04 09:04:25 +00:00
except Exception:
pass
2015-11-07 05:38:50 +00:00
if self.channel in r:
2015-11-04 09:04:25 +00:00
try:
2015-11-07 05:38:50 +00:00
x = self.channel.recv(1024)
2015-11-04 09:04:25 +00:00
if len(x) == 0:
break
sys.stdout.write(x)
sys.stdout.flush()
now_timestamp = time.time()
log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(x)))
2015-11-07 05:38:50 +00:00
log_file_f.write(x)
2015-11-04 09:04:25 +00:00
pre_timestamp = now_timestamp
log_file_f.flush()
log_time_f.flush()
2015-11-07 05:38:50 +00:00
if input_mode and not self.is_output(x):
data += x
2015-11-04 09:04:25 +00:00
except socket.timeout:
pass
if sys.stdin in r:
x = os.read(sys.stdin.fileno(), 1)
input_mode = True
2015-11-04 09:04:25 +00:00
if str(x) in ['\r', '\n', '\r\n']:
2015-11-16 15:33:44 +00:00
data = self.deal_command(data, self.ssh)
2015-11-07 05:38:50 +00:00
TtyLog(log=log, datetime=datetime.datetime.now(), cmd=data).save()
data = ''
2015-11-04 09:04:25 +00:00
input_mode = False
if len(x) == 0:
break
2015-11-07 05:38:50 +00:00
self.channel.send(x)
2015-11-04 09:04:25 +00:00
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty)
log_file_f.write('End time is %s' % datetime.datetime.now())
log_file_f.close()
log.is_finished = True
log.end_time = datetime.datetime.now()
log.save()
def connect(self):
"""
Connect server.
连接服务器
"""
ps1 = "PS1='[\u@%s \W]\$ '\n" % self.ip
login_msg = "clear;echo -e '\\033[32mLogin %s done. Enjoy it.\\033[0m'\n" % self.ip
# 发起ssh连接请求 Make a ssh connection
ssh = self.get_connection()
# 获取连接的隧道并设置窗口大小 Make a channel and set windows size
global channel
win_size = self.get_win_size()
2015-11-07 05:38:50 +00:00
self.channel = channel = ssh.invoke_shell(height=win_size[0], width=win_size[1], term='xterm')
2015-11-04 09:04:25 +00:00
try:
signal.signal(signal.SIGWINCH, self.set_win_size)
except:
pass
# 设置PS1并提示 Set PS1 and msg it
#channel.send(ps1)
#channel.send(login_msg)
2015-11-07 05:38:50 +00:00
# channel.send('echo ${SSH_TTY}\n')
# global SSH_TTY
# while not channel.recv_ready():
# time.sleep(1)
# tmp = channel.recv(1024)
2015-11-04 09:04:25 +00:00
#print 'ok'+tmp+'ok'
# SSH_TTY = re.search(r'(?<=/dev/).*', tmp).group().strip()
2015-11-07 05:38:50 +00:00
# SSH_TTY = ''
2015-11-18 07:15:08 +00:00
# channel.send('clear\n')
2015-11-04 09:04:25 +00:00
# Make ssh interactive tunnel
2015-11-07 05:38:50 +00:00
self.posix_shell()
2015-11-04 09:04:25 +00:00
# Shutdown channel socket
channel.close()
ssh.close()
def execute(self, cmd):
"""
execute cmd on the asset
执行命令
"""
pass
2015-11-18 07:15:08 +00:00
def print_user_asset_group_info(user):
asset_groups = AssetGroup.objects.all()
for asset_group in asset_groups:
if asset_group.comment:
print '[%-2s] %-10s %s' % (asset_group.id, asset_group.name, asset_group.comment)
else:
print '[%-2s] %-10s' % (asset_group.id, asset_group.name)
print
class Nav(object):
def __init__(self, user):
self.user = user
self.search_result = {}
@staticmethod
def print_nav():
"""
Print prompt
打印提示导航
"""
msg = """\n\033[1;32m### Welcome To Use JumpServer, A Open Source System . ### \033[0m
1) Type \033[32mID\033[0m To Login.
2) Type \033[32m/\033[0m + \033[32mIP, Host Name, Host Alias or Comments \033[0mTo Search.
3) Type \033[32mP/p\033[0m To Print The Servers You Available.
4) Type \033[32mG/g\033[0m To Print The Server Groups You Available.
5) Type \033[32mG/g\033[0m\033[0m + \033[32mGroup ID\033[0m To Print The Server Group You Available.
6) Type \033[32mE/e\033[0m To Execute Command On Several Servers.
7) Type \033[32mQ/q\033[0m To Quit.
"""
msg = """\n\033[1;32m### 欢迎使用Jumpserver开源跳板机 ### \033[0m
1) 输入 \033[32mID\033[0m 直接登录.
2) 输入 \033[32m/\033[0m + \033[32mIP, 主机名, 主机别名 or 备注 \033[0m搜索.
3) 输入 \033[32mP/p\033[0m 显示您有权限的主机.
4) 输入 \033[32mG/g\033[0m 显示您有权限的主机组.
5) 输入 \033[32mG/g\033[0m\033[0m + \033[32m组ID\033[0m 显示该组下主机.
6) 输入 \033[32mE/e\033[0m 批量执行命令.
7) 输入 \033[32mQ/q\033[0m 退出.
"""
print textwrap.dedent(msg)
def search(self, str_r=''):
gid_pattern = re.compile(r'^g\d+$')
user_asset_all = list(Asset.objects.all())
user_asset_search = []
if str_r:
if gid_pattern.match(str_r):
user_asset_search = list(Asset.objects.all())
else:
for asset in user_asset_all:
if str_r in asset.ip or str_r in str(asset.comment):
user_asset_search.append(asset)
else:
user_asset_search = user_asset_all
self.search_result = dict(zip(range(len(user_asset_search)), user_asset_search))
print '\033[32m[%-3s] %-15s %-15s %-5s %-5s %s \033[0m' % ('ID', 'AssetName', 'IP', 'Port', 'Role', 'Comment')
for index, asset in self.search_result.items():
if asset.comment:
print '[%-3s] %-15s %-15s %-5s %-5s %s' % (index, 'asset_name'+str(index), asset.ip, asset.port, 'role', asset.comment)
else:
print '[%-3s] %-15s %-15s %-5s %-5s' % (index, 'asset_name'+str(index), asset.ip, asset.port, 'role')
print
@staticmethod
def print_asset_group():
user_asset_group_all = AssetGroup.objects.all()
print '\033[32m[%-3s] %-15s %s \033[0m' % ('ID', 'GroupName', 'Comment')
for asset_group in user_asset_group_all:
if asset_group.comment:
print '[%-3s] %-15s %s' % (asset_group.id, asset_group.name, asset_group.comment)
else:
print '[%-3s] %-15s' % (asset_group.id, asset_group.name)
print
2015-04-20 02:12:11 +00:00
2015-08-21 15:45:41 +00:00
def main():
2015-08-26 15:31:32 +00:00
"""
he he
主程序
"""
2015-08-21 15:45:41 +00:00
if not login_user: # 判断用户是否存在
color_print(u'没有该用户或许你是以root运行的 No that user.', exits=True)
2015-06-09 15:06:32 +00:00
gid_pattern = re.compile(r'^g\d+$')
2015-11-18 07:15:08 +00:00
nav = Nav(login_user)
nav.print_nav()
2015-08-21 15:45:41 +00:00
2015-04-20 02:12:11 +00:00
try:
while True:
try:
2015-11-18 07:15:08 +00:00
option = raw_input("\033[1;32mOpt or ID>:\033[0m ").strip()
2015-04-20 02:12:11 +00:00
except EOFError:
2015-11-18 07:15:08 +00:00
nav.print_nav()
2015-04-20 02:12:11 +00:00
continue
except KeyboardInterrupt:
sys.exit(0)
2015-11-18 07:15:08 +00:00
if option in ['P', 'p', '\n', '']:
nav.search()
2015-04-20 02:12:11 +00:00
continue
2015-11-18 07:15:08 +00:00
if option.startswith('/') or gid_pattern.match(option):
nav.search(option.lstrip('/'))
2015-04-20 02:12:11 +00:00
elif option in ['G', 'g']:
2015-11-18 07:15:08 +00:00
nav.print_asset_group()
2015-04-20 02:12:11 +00:00
continue
elif option in ['E', 'e']:
2015-06-15 15:00:19 +00:00
# exec_cmd_servers(login_name)
pass
2015-04-20 02:12:11 +00:00
elif option in ['Q', 'q', 'exit']:
sys.exit()
else:
try:
2015-11-18 07:15:08 +00:00
asset = nav.search_result[int(option)]
ssh_tty = SshTty('a', 'b')
ssh_tty.connect()
except (KeyError, ValueError):
color_print('请输入正确ID', 'red')
2015-04-20 02:12:11 +00:00
except ServerError, e:
color_print(e, 'red')
except IndexError:
pass
2015-08-21 15:45:41 +00:00
if __name__ == '__main__':
main()