modify some api.py bug

pull/26/head
ibuler 2015-06-13 00:36:10 +08:00
parent 5d16836b05
commit fdcaa358e5
2 changed files with 118 additions and 60 deletions

View File

@ -244,32 +244,37 @@ def posix_shell(chan, username, host):
def verify_connect(username, part_ip):
pass
# ip_matched = []
# try:
# assets = get_asset(username=username)
# except ServerError, e:
# color_print(e, 'red')
# return False
#
# assets_info =
# for ip_info in hosts:
# if part_ip in ip_info[1:] and part_ip:
# ip_matched = [ip_info[1]]
# break
# for info in 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:
# for ip in ip_matched:
# print '%-15s -- %s' % (ip, hosts_attr[ip][2])
# elif len(ip_matched) < 1:
# color_print('No Permission or No host.', 'red')
# else:
# username, password, host, port = get_connect_item(username, ip_matched[0])
# connect(username, password, host, port, login_name)
ip_matched = []
try:
assets_info = user.get_asset_info()
except ServerError, e:
color_print(e, 'red')
return False
for ip, asset_info in assets_info.items():
if part_ip in asset_info[1:] and part_ip:
ip_matched = [asset_info[1]]
break
for info in asset_info[1:]:
if part_ip in info:
ip_matched.append(ip)
logger.debug('%s matched input %s: %s' % (user.username, part_ip, ip_matched))
ip_matched = list(set(ip_matched))
if len(ip_matched) > 1:
for ip in ip_matched:
if assets_info[ip][2]:
print '%-15s -- %s' % (ip, assets_info[ip][2])
else:
print '%-15s' % ip
print ''
elif len(ip_matched) < 1:
color_print('No Permission or No host.', 'red')
else:
username, password, host, port = get_connect_item(username, ip_matched[0])
connect(username, password, host, port, login_name)
def print_prompt():
@ -437,7 +442,8 @@ if __name__ == '__main__':
elif gid_pattern.match(option):
gid = option[1:].strip()
asset_group = JassetGroup(id=gid)
asset_group.get_asset_info(printable=True)
if asset_group.validate():
asset_group.get_asset_info(printable=True)
continue
elif option in ['E', 'e']:
exec_cmd_servers(login_name)

View File

@ -301,6 +301,7 @@ class Juser(object):
Jumpserver user class
用户类
"""
def __init__(self, username=None, uid=None):
if username:
user = User.objects.filter(username=username)
@ -313,16 +314,31 @@ class Juser(object):
user = user[0]
self.user = user
self.id = user.id
self.username = user.username
self.name = user.name
# self.id = user.id
# self.username = user.username
# self.name = user.name
self.group = user.group.all()
else:
self.id = None
def __repr__(self):
if self.id:
return '<%s Juser instance>' % getattr(self.user, 'username')
else:
return 'None'
def __getattr__(self, item):
if self.id:
return getattr(self.user, item)
else:
return None
def validate(self):
"""
Validate is or not a true user
鉴定用户
"""
if self.user:
if self.id:
return True
else:
return False
@ -362,21 +378,22 @@ class Juser(object):
asset_groups_info[group_id][1])
else:
print "[%3s] %s" % (group_id, asset_groups_info[group_id][0])
print ''
else:
return asset_groups_info
def get_asset(self):
"""
Get the hosts of under the user control.
Get the assets of under the user control.
获取主机列表
"""
hosts = []
host_groups = self.get_asset_group()
assets = []
asset_groups = self.get_asset_group()
for host_group in host_groups:
hosts.extend(get_asset_group_member(host_group.id))
for asset_group in asset_groups:
assets.extend(asset_group.asset_set.all())
return hosts
return assets
def get_asset_info(self, printable=False):
"""
@ -389,9 +406,9 @@ class Juser(object):
for asset in assets:
asset_alias = AssetAlias.objects.filter(user=self.user, asset=asset)
if asset_alias and asset_alias[0].alias != '':
assets_info[asset.ip] = [asset.id, asset.ip, asset_alias[0].alias]
assets_info[asset.ip] = [asset.id, asset.ip, str(asset_alias[0].alias)]
else:
assets_info[asset.ip] = [asset.id, asset.ip, asset.comment]
assets_info[asset.ip] = [asset.id, asset.ip, str(asset.comment)]
if printable:
ips = assets_info.keys()
@ -407,6 +424,7 @@ class Juser(object):
class Jasset(object):
def __init__(self, ip=None, id=None):
if ip:
asset = Asset.objects.filter(ip=ip)
@ -418,20 +436,40 @@ class Jasset(object):
if asset:
asset = asset[0]
self.asset = asset
self.ip = asset.ip
self.id = asset.id
self.port = asset.port
self.comment = asset.comment
# self.ip = asset.ip
# self.id = asset.id
# self.port = asset.port
# self.comment = asset.comment
else:
self.id = None
def __repr__(self):
if self.id:
return '<%s Jasset instance>' % self.asset.ip
else:
return 'None'
def __getattr__(self, item):
if self.id:
return getattr(self.asset, item)
else:
return None
def validate(self):
if self.asset:
"""
Validate is or not a true asset
判断是否存在
"""
if self.id:
return True
else:
return False
class JassetGroup(object):
def __init__(self, id=None, name=None):
def __init__(self, name=None, id=None):
if id:
asset_group = BisGroup.objects.filter(id=int(id))
elif name:
@ -442,11 +480,23 @@ class JassetGroup(object):
if asset_group:
asset_group = asset_group[0]
self.asset_group = asset_group
self.name = asset_group.name
# self.name = asset_group.name
self.id = asset_group.id
else:
self.id = None
def __repr__(self):
if self.id:
return '<%s JassetGroup instance>' % self.name
else:
return 'None'
def validate(self):
if self.asset_group:
"""
Validate it is a true asset group or not
鉴定是否为真是存在的组
"""
if self.id:
return True
else:
return False
@ -457,14 +507,16 @@ class JassetGroup(object):
def get_asset_info(self, printable=False):
assets = self.get_asset()
for asset in assets:
print '%-15s -- %s' % (asset.ip, asset.comment)
if asset.comment:
print '%-15s -- %s' % (asset.ip, asset.comment)
else:
print '%-15s' % asset.ip
print ''
def get_asset_num(self):
return len(self.get_asset())
# def get_asset_group(user=None):
# """
# Get user host_groups.
@ -484,18 +536,18 @@ class JassetGroup(object):
# return host_group_list
def get_asset_group_member(gid):
"""
Get host_group's member host
获取主机组下的主机
"""
hosts = []
if gid:
host_group = BisGroup.objects.filter(id=gid)
if host_group:
host_group = host_group[0]
hosts = host_group.asset_set.all()
return hosts
# def get_asset_group_member(gid):
# """
# Get host_group's member host
# 获取主机组下的主机
# """
# hosts = []
# if gid:
# host_group = BisGroup.objects.filter(id=gid)
# if host_group:
# host_group = host_group[0]
# hosts = host_group.asset_set.all()
# return hosts
# def get_asset(user=None):