jumpserver/connect.py

149 lines
4.2 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
import textwrap
import getpass
import readline
2015-06-08 15:46:40 +00:00
import django
2015-04-20 02:12:11 +00:00
from multiprocessing import Pool
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
2015-06-08 15:46:40 +00:00
if django.get_version() != '1.6':
django.setup()
2015-08-26 15:31:32 +00:00
from jumpserver.api import ServerError, User, Asset, Jtty, get_object
from jumpserver.api import logger
2015-04-24 10:19:43 +00:00
2015-08-21 15:45:41 +00:00
login_user = get_object(User, username=getpass.getuser())
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-06-15 15:00:19 +00:00
def verify_connect(user, option):
2015-08-26 15:31:32 +00:00
"""
Check user was permed or not . Check ip is unique or not.
鉴定用户是否有该主机权限 匹配到的ip是否唯一
"""
2015-06-12 16:36:10 +00:00
ip_matched = []
try:
2015-06-15 11:20:05 +00:00
assets_info = login_user.get_asset_info()
2015-06-12 16:36:10 +00:00
except ServerError, e:
color_print(e, 'red')
return False
for ip, asset_info in assets_info.items():
2015-06-15 15:00:19 +00:00
if option in asset_info[1:] and option:
2015-06-12 16:36:10 +00:00
ip_matched = [asset_info[1]]
break
for info in asset_info[1:]:
2015-06-15 15:00:19 +00:00
if option in info:
2015-06-12 16:36:10 +00:00
ip_matched.append(ip)
2015-06-15 15:00:19 +00:00
logger.debug('%s matched input %s: %s' % (login_user.username, option, ip_matched))
2015-06-12 16:36:10 +00:00
ip_matched = list(set(ip_matched))
2015-08-26 15:31:32 +00:00
if len(ip_matched) > 1: # 如果匹配ip不唯一
2015-08-21 15:45:41 +00:00
ip_comment = {}
2015-06-12 16:36:10 +00:00
for ip in ip_matched:
2015-08-21 15:45:41 +00:00
ip_comment[ip] = assets_info[ip][2]
for ip in sorted(ip_comment):
if ip_comment[ip]:
print '%-15s -- %s' % (ip, ip_comment[ip])
2015-06-12 16:36:10 +00:00
else:
print '%-15s' % ip
2015-08-21 15:45:41 +00:00
print ''
2015-08-26 15:31:32 +00:00
elif len(ip_matched) < 1: # 如果没匹配到
2015-08-21 15:45:41 +00:00
color_print('没有该主机,或者您没有该主机的权限 No Permission or No host.', 'red')
2015-08-26 15:31:32 +00:00
else: # 恰好是1个
2015-08-21 15:45:41 +00:00
asset = get_object(Asset, ip=ip_matched[0])
2015-06-16 01:21:11 +00:00
jtty = Jtty(user, asset)
jtty.connect()
2015-04-20 02:12:11 +00:00
def print_prompt():
2015-08-26 15:31:32 +00:00
"""
Print prompt
打印提示导航
"""
2015-04-20 02:12:11 +00:00
msg = """\033[1;32m### Welcome Use JumpServer To Login. ### \033[0m
2015-04-24 10:19:43 +00:00
1) Type \033[32mIP or Part IP, Host Alias or Comments \033[0m To Login.
2015-04-20 02:12:11 +00:00
2) Type \033[32mP/p\033[0m To Print The Servers You Available.
3) Type \033[32mG/g\033[0m To Print The Server Groups You Available.
4) Type \033[32mG/g(1-N)\033[0m To Print The Server Group Hosts You Available.
5) Type \033[32mE/e\033[0m To Execute Command On Several Servers.
6) Type \033[32mQ/q\033[0m To Quit.
"""
print textwrap.dedent(msg)
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
2015-04-20 02:12:11 +00:00
print_prompt()
gid_pattern = re.compile(r'^g\d+$')
2015-08-21 15:45:41 +00:00
2015-04-20 02:12:11 +00:00
try:
while True:
try:
option = raw_input("\033[1;32mOpt or IP>:\033[0m ")
except EOFError:
2015-06-10 15:16:24 +00:00
print_prompt()
2015-04-20 02:12:11 +00:00
continue
except KeyboardInterrupt:
sys.exit(0)
2015-04-20 02:12:11 +00:00
if option in ['P', 'p']:
2015-06-15 11:20:05 +00:00
login_user.get_asset_info(printable=True)
2015-04-20 02:12:11 +00:00
continue
elif option in ['G', 'g']:
2015-06-15 11:20:05 +00:00
login_user.get_asset_group_info(printable=True)
2015-04-20 02:12:11 +00:00
continue
elif gid_pattern.match(option):
2015-04-20 02:12:11 +00:00
gid = option[1:].strip()
2015-08-21 15:45:41 +00:00
asset_group = get_object(AssetGroup, id=gid)
if asset_group and asset_group.is_permed(user=login_user):
2015-06-12 16:36:10 +00:00
asset_group.get_asset_info(printable=True)
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-06-15 15:00:19 +00:00
verify_connect(login_user, option)
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()