Commit fdcaa358 authored by ibuler's avatar ibuler

modify some api.py bug

parent 5d16836b
...@@ -244,32 +244,37 @@ def posix_shell(chan, username, host): ...@@ -244,32 +244,37 @@ def posix_shell(chan, username, host):
def verify_connect(username, part_ip): def verify_connect(username, part_ip):
pass ip_matched = []
# ip_matched = [] try:
# try: assets_info = user.get_asset_info()
# assets = get_asset(username=username) except ServerError, e:
# except ServerError, e: color_print(e, 'red')
# color_print(e, 'red') return False
# return False
# for ip, asset_info in assets_info.items():
# assets_info = if part_ip in asset_info[1:] and part_ip:
# for ip_info in hosts: ip_matched = [asset_info[1]]
# if part_ip in ip_info[1:] and part_ip: break
# ip_matched = [ip_info[1]]
# break for info in asset_info[1:]:
# for info in ip_info[1:]: if part_ip in info:
# if part_ip in info: ip_matched.append(ip)
# ip_matched.append(ip_info[1])
# logger.debug('%s matched input %s: %s' % (user.username, part_ip, ip_matched))
# ip_matched = list(set(ip_matched)) ip_matched = list(set(ip_matched))
# if len(ip_matched) > 1:
# 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: if assets_info[ip][2]:
# color_print('No Permission or No host.', 'red') print '%-15s -- %s' % (ip, assets_info[ip][2])
# else: else:
# username, password, host, port = get_connect_item(username, ip_matched[0]) print '%-15s' % ip
# connect(username, password, host, port, login_name) 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(): def print_prompt():
...@@ -437,7 +442,8 @@ if __name__ == '__main__': ...@@ -437,7 +442,8 @@ if __name__ == '__main__':
elif gid_pattern.match(option): elif gid_pattern.match(option):
gid = option[1:].strip() gid = option[1:].strip()
asset_group = JassetGroup(id=gid) asset_group = JassetGroup(id=gid)
asset_group.get_asset_info(printable=True) if asset_group.validate():
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)
......
...@@ -301,6 +301,7 @@ class Juser(object): ...@@ -301,6 +301,7 @@ class Juser(object):
Jumpserver user class 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)
...@@ -313,16 +314,31 @@ class Juser(object): ...@@ -313,16 +314,31 @@ class Juser(object):
user = user[0] user = user[0]
self.user = user self.user = user
self.id = user.id self.id = user.id
self.username = user.username # self.id = user.id
self.name = user.name # self.username = user.username
# self.name = user.name
self.group = user.group.all() 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): def validate(self):
""" """
Validate is or not a true user Validate is or not a true user
鉴定用户 鉴定用户
""" """
if self.user: if self.id:
return True return True
else: else:
return False return False
...@@ -362,21 +378,22 @@ class Juser(object): ...@@ -362,21 +378,22 @@ class Juser(object):
asset_groups_info[group_id][1]) asset_groups_info[group_id][1])
else: else:
print "[%3s] %s" % (group_id, asset_groups_info[group_id][0]) print "[%3s] %s" % (group_id, asset_groups_info[group_id][0])
print ''
else: else:
return asset_groups_info return asset_groups_info
def get_asset(self): def get_asset(self):
""" """
Get the hosts of under the user control. Get the assets of under the user control.
获取主机列表 获取主机列表
""" """
hosts = [] assets = []
host_groups = self.get_asset_group() asset_groups = self.get_asset_group()
for host_group in host_groups: for asset_group in asset_groups:
hosts.extend(get_asset_group_member(host_group.id)) assets.extend(asset_group.asset_set.all())
return hosts return assets
def get_asset_info(self, printable=False): def get_asset_info(self, printable=False):
""" """
...@@ -389,9 +406,9 @@ class Juser(object): ...@@ -389,9 +406,9 @@ class Juser(object):
for asset in assets: for asset in assets:
asset_alias = AssetAlias.objects.filter(user=self.user, asset=asset) asset_alias = AssetAlias.objects.filter(user=self.user, asset=asset)
if asset_alias and asset_alias[0].alias != '': 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: else:
assets_info[asset.ip] = [asset.id, asset.ip, asset.comment] assets_info[asset.ip] = [asset.id, asset.ip, str(asset.comment)]
if printable: if printable:
ips = assets_info.keys() ips = assets_info.keys()
...@@ -407,6 +424,7 @@ class Juser(object): ...@@ -407,6 +424,7 @@ class Juser(object):
class Jasset(object): class Jasset(object):
def __init__(self, ip=None, id=None): def __init__(self, ip=None, id=None):
if ip: if ip:
asset = Asset.objects.filter(ip=ip) asset = Asset.objects.filter(ip=ip)
...@@ -418,20 +436,40 @@ class Jasset(object): ...@@ -418,20 +436,40 @@ class Jasset(object):
if asset: if asset:
asset = asset[0] asset = asset[0]
self.asset = asset self.asset = asset
self.ip = asset.ip
self.id = asset.id self.id = asset.id
self.port = asset.port # self.ip = asset.ip
self.comment = asset.comment # 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): def validate(self):
if self.asset: """
Validate is or not a true asset
判断是否存在
"""
if self.id:
return True return True
else: else:
return False return False
class JassetGroup(object): class JassetGroup(object):
def __init__(self, id=None, name=None):
def __init__(self, name=None, id=None):
if id: if id:
asset_group = BisGroup.objects.filter(id=int(id)) asset_group = BisGroup.objects.filter(id=int(id))
elif name: elif name:
...@@ -442,11 +480,23 @@ class JassetGroup(object): ...@@ -442,11 +480,23 @@ class JassetGroup(object):
if asset_group: if asset_group:
asset_group = asset_group[0] asset_group = asset_group[0]
self.asset_group = asset_group self.asset_group = asset_group
self.name = asset_group.name # self.name = asset_group.name
self.id = asset_group.id 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): def validate(self):
if self.asset_group: """
Validate it is a true asset group or not
鉴定是否为真是存在的组
"""
if self.id:
return True return True
else: else:
return False return False
...@@ -457,14 +507,16 @@ class JassetGroup(object): ...@@ -457,14 +507,16 @@ class JassetGroup(object):
def get_asset_info(self, printable=False): def get_asset_info(self, printable=False):
assets = self.get_asset() assets = self.get_asset()
for asset in assets: 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): def get_asset_num(self):
return len(self.get_asset()) return len(self.get_asset())
# def get_asset_group(user=None): # def get_asset_group(user=None):
# """ # """
# Get user host_groups. # Get user host_groups.
...@@ -484,18 +536,18 @@ class JassetGroup(object): ...@@ -484,18 +536,18 @@ class JassetGroup(object):
# return host_group_list # return host_group_list
def get_asset_group_member(gid): # def get_asset_group_member(gid):
""" # """
Get host_group's member host # Get host_group's member host
获取主机组下的主机 # 获取主机组下的主机
""" # """
hosts = [] # hosts = []
if gid: # if gid:
host_group = BisGroup.objects.filter(id=gid) # host_group = BisGroup.objects.filter(id=gid)
if host_group: # if host_group:
host_group = host_group[0] # host_group = host_group[0]
hosts = host_group.asset_set.all() # hosts = host_group.asset_set.all()
return hosts # return hosts
# def get_asset(user=None): # def get_asset(user=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment