modify connect.py

pull/26/head
ibuler 2015-06-10 23:16:24 +08:00
parent fc22677f3f
commit 5d16836b05
2 changed files with 79 additions and 43 deletions

View File

@ -27,7 +27,7 @@ if django.get_version() != '1.6':
django.setup() django.setup()
from juser.models import User from juser.models import User
from jlog.models import Log from jlog.models import Log
from jumpserver.api import CONF, BASE_DIR, ServerError, Juser from jumpserver.api import CONF, BASE_DIR, ServerError, Juser, JassetGroup
from jumpserver.api import AssetAlias, get_connect_item, logger from jumpserver.api import AssetAlias, get_connect_item, logger
try: try:
@ -244,31 +244,32 @@ def posix_shell(chan, username, host):
def verify_connect(username, part_ip): def verify_connect(username, part_ip):
ip_matched = [] pass
try: # ip_matched = []
assets = get_asset(username=username) # try:
except ServerError, e: # assets = get_asset(username=username)
color_print(e, 'red') # except ServerError, e:
return False # color_print(e, 'red')
# return False
assets_info = #
for ip_info in hosts: # assets_info =
if part_ip in ip_info[1:] and part_ip: # for ip_info in hosts:
ip_matched = [ip_info[1]] # if part_ip in ip_info[1:] and part_ip:
break # ip_matched = [ip_info[1]]
for info in ip_info[1:]: # break
if part_ip in info: # for info in ip_info[1:]:
ip_matched.append(ip_info[1]) # if part_ip in info:
# ip_matched.append(ip_info[1])
ip_matched = list(set(ip_matched)) #
if len(ip_matched) > 1: # ip_matched = list(set(ip_matched))
for ip in ip_matched: # if len(ip_matched) > 1:
print '%-15s -- %s' % (ip, hosts_attr[ip][2]) # for ip in ip_matched:
elif len(ip_matched) < 1: # print '%-15s -- %s' % (ip, hosts_attr[ip][2])
color_print('No Permission or No host.', 'red') # elif len(ip_matched) < 1:
else: # color_print('No Permission or No host.', 'red')
username, password, host, port = get_connect_item(username, ip_matched[0]) # else:
connect(username, password, host, port, login_name) # username, password, host, port = get_connect_item(username, ip_matched[0])
# connect(username, password, host, port, login_name)
def print_prompt(): def print_prompt():
@ -289,18 +290,18 @@ def print_prompt():
# print "[%3s] %s -- %s" % (host_group.id, host_group.ip, host_group.comment) # print "[%3s] %s -- %s" % (host_group.id, host_group.ip, host_group.comment)
def asset_group_member(username, gid): # def asset_group_member(username, gid):
pattern = re.compile(r'\d+') # pattern = re.compile(r'\d+')
match = pattern.match(gid) # match = pattern.match(gid)
#
if match: # if match:
hosts_attr = get_host_group_host(username, gid) # hosts_attr = get_host_group_host(username, gid)
hosts = hosts_attr.keys() # hosts = hosts_attr.keys()
hosts.sort() # hosts.sort()
for ip in hosts: # for ip in hosts:
print '%-15s -- %s' % (ip, hosts_attr[ip][2]) # print '%-15s -- %s' % (ip, hosts_attr[ip][2])
else: # else:
color_print('No such group id, Please check it.', 'red') # color_print('No such group id, Please check it.', 'red')
def connect(username, password, host, port, login_name): def connect(username, password, host, port, login_name):
@ -373,13 +374,13 @@ def multi_remote_exec_cmd(hosts, username, cmd):
def exec_cmd_servers(username): def exec_cmd_servers(username):
color_print("You can choose in the following IP(s), Use glob or ips split by comma. q/Q to PreLayer.", 'green') color_print("You can choose in the following IP(s), Use glob or ips split by comma. q/Q to PreLayer.", 'green')
print_user_host(login_name) user.get_asset_info(printable=True)
while True: while True:
hosts = [] hosts = []
inputs = raw_input('\033[1;32mip(s)>: \033[0m') inputs = raw_input('\033[1;32mip(s)>: \033[0m')
if inputs in ['q', 'Q']: if inputs in ['q', 'Q']:
break break
get_hosts = get_user_host(username).keys() get_hosts = user.get_asset_info().keys()
if ',' in inputs: if ',' in inputs:
ips_input = inputs.split(',') ips_input = inputs.split(',')
@ -423,7 +424,7 @@ if __name__ == '__main__':
try: try:
option = raw_input("\033[1;32mOpt or IP>:\033[0m ") option = raw_input("\033[1;32mOpt or IP>:\033[0m ")
except EOFError: except EOFError:
print print_prompt()
continue continue
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(0) sys.exit(0)
@ -435,7 +436,8 @@ if __name__ == '__main__':
continue continue
elif gid_pattern.match(option): elif gid_pattern.match(option):
gid = option[1:].strip() gid = option[1:].strip()
print_user_hostgroup_host(login_name, gid) asset_group = JassetGroup(id=gid)
asset_group.get_asset_info(printable=True)
continue continue
elif option in ['E', 'e']: elif option in ['E', 'e']:
exec_cmd_servers(login_name) exec_cmd_servers(login_name)

View File

@ -297,6 +297,10 @@ def user_group_perm_asset_group_api(user_group):
class Juser(object): class Juser(object):
"""
Jumpserver user class
用户类
"""
def __init__(self, username=None, uid=None): def __init__(self, username=None, uid=None):
if username: if username:
user = User.objects.filter(username=username) user = User.objects.filter(username=username)
@ -427,7 +431,37 @@ class Jasset(object):
class JassetGroup(object): class JassetGroup(object):
pass def __init__(self, id=None, name=None):
if id:
asset_group = BisGroup.objects.filter(id=int(id))
elif name:
asset_group = BisGroup.objects.filter(name=name)
else:
asset_group = ''
if asset_group:
asset_group = asset_group[0]
self.asset_group = asset_group
self.name = asset_group.name
self.id = asset_group.id
def validate(self):
if self.asset_group:
return True
else:
return False
def get_asset(self):
return self.asset_group.asset_set.all()
def get_asset_info(self, printable=False):
assets = self.get_asset()
for asset in assets:
print '%-15s -- %s' % (asset.ip, asset.comment)
def get_asset_num(self):
return len(self.get_asset())